Bluetooth services is not started



  • good afternoon i have one problem at server side
    services=conn.services() its not working what is the problem
    client.py

    def ble_reciever(self):
    
        bluetooth = Bluetooth()
        print("Scanning 4509 client")
        bluetooth.start_scan(-1)
    
        while True:
            adv = bluetooth.get_adv()
            if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'LoPy':
                print("connecting rr Lopy in 4509 client")
                conn = bluetooth.connect(adv.mac)
                print("connectede w Lopy in 4509 client")
                while True:
                    print("getrdasdf  servicess..")
                    conn = bluetooth.connect(adv.mac)
                    services = conn.services()
                    print("get servicess..")
                    for service in services:
                        #time.sleep(3)
                        print("start reading..")
                        if type(service.uuid()) == bytes:
                            print('Reading chars from service = {}'.format(service.uuid()))
                        else:
                            print('Reading chars from service = %x' % service.uuid())
                        chars = service.characteristics()
                        for char in chars:
                            if (char.properties() & Bluetooth.PROP_READ):
                                print('char {} value = {}'.format(char.uuid(), char.read()))
                conn.disconnect()
                print("Disconnected 4509 client")
                break
            else:
                time.sleep(0.050)


  • @jmahesh

    Hi,

    I tried it too and it's working.

    Thank You!



  • @nagarjuna-reddy said in Bluetooth services is not started:

    conn.disconnect()

    Hi,
    put a sleep time before service loop, that will make the services to read. I tried and it is working.

    time.sleep(1.0);
    print('sleep resumed', conn.services())
    services = conn.services()
    print(services)
    for service in conn.services():



  • @st3ty

    hi i am also have a same problem if you success to execute please send your code to me.
    Thank you



  • @livius

    ivius
    you already edit my code but same problem i am getting what iam expected output is sending before message so please check and replay it urgently.



  • @nagarjuna-reddy
    livius you already edit my code but same problem i am getting what iam expected output is sending before message so please check and replay it arguently.



  • @livius said in Bluetooth services is not started:

    Bluetooth.CHAR_READ_EVENT
    0_1511346140454_Capture.JPG

    transmitter run in one LoPy and reciever LoPy is connected but not get any values from transmitter side i want to
    bolded text

    i=0
    while True:
    val = 'Value{}'.format(i)
    print('Setting value: {}'.format(val))
    chr1.value(val) #Set the characteristic value - should trigger notification is a client has registered for notifications
    time.sleep(3)
    i+=1
    this is a transmitter side loop this loop values is getting to once connected reciever is recieved that for loop values but in their not iam getting so please solve my problem



  • i only mean that you must connect only once and for one connect you need one disconnect
    below "fixed" code in this matter
    i have also added small sleep time - look if it help somehow with your problem (i do not tested this self)

    def ble_reciever(self):
    		bluetooth = Bluetooth()
    		print("Scanning 4509 client")
    		bluetooth.start_scan(-1)
    
    		while True:
    			adv = bluetooth.get_adv()
    			#if adv:
    				#print("Found adv in 4509 client")
    				# try to get the complete name
    				#print(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL))
    
    				# try to get the manufacturer data (Apple's iBeacon data is sent here)
    				#print(binascii.hexlify(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_MANUFACTURER_DATA)))
    
    
    			if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'LoPy':
    				print("connecting rr Lopy in 4509 client")					
    				conn = bluetooth.connect(adv.mac)					
    				print("connected Lopy in 4509 client")  	
    				
    				services = conn.services()				 
    				for service in services:
    					time.sleep(0.050)
    					print("start reading..")
    					if type(service.uuid()) == bytes:
    						print('Reading chars from service = {}'.format(service.uuid()))
    					else:
    						print('Reading chars from service = %x' % service.uuid())
    					chars = service.characteristics()
    					for char in chars:
    						if (char.properties() & Bluetooth.PROP_READ):
    							print('char {} value = {}'.format(char.uuid(), char.read()))
    							
    				conn.disconnect()
    				print("Disconnected 4509 client")
    				break
    			else:
    				time.sleep(0.050)
    
    	def ble_transmitter(self):
    
    		print('Advertising Bluetooth...')
    		bluetooth = Bluetooth()
    		bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890abcdef')
    
    		def conn_cb (bt_o):
    			events = bt_o.events()
    			if  events & Bluetooth.CLIENT_CONNECTED:
    				print("Client connected")
    			elif events & Bluetooth.CLIENT_DISCONNECTED:
    				print("Client disconnected")
    
    		bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    		bluetooth.advertise(True)
    
    		#Create service and characteristic
    		srv1 = bluetooth.service(uuid='0000000000000000', isprimary=True)
    		chr1 = srv1.characteristic(uuid='0000000000000002', properties=Bluetooth.PROP_INDICATE | Bluetooth.PROP_BROADCAST | Bluetooth.PROP_READ | Bluetooth.PROP_NOTIFY, value='InitialValue')
    
    		#Create callback for read events on the characteristic
    		def char1_cb(chr):
    			events = chr.events()
    			if events & Bluetooth.CHAR_READ_EVENT :
    				print('Bluetooth.CHAR_READ_EVENT')
    
    		chr1.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char1_cb)
    
    		#Update the characteristic value very few seconds
    		#i=0
    		while True:
    			val = 'Value{}'.format(i)
    			print('Setting value: {}'.format(val))
    			chr1.value(val) #Set the characteristic value - should trigger notification is a client has registered for notifications
    			time.sleep(3)
    			i+=1
    


  • @livius
    i cannot understand livius so edit my code and send to me please........



  • I have the exact same problem.

    I am trying to connect two Wipys via bluetooth.
    I get the connection on both sides.
    But the programm frezzes with the line services = connection.services()

    Is this a bug or is it a problem with my code?

    from network import Bluetooth
    import binascii
    
    bluetooth = Bluetooth()
    
    bluetooth.start_scan(-1)
    
    adv = None
    
    while True:
    
    
        adv = bluetooth.get_adv()
    
        if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'WiPy':
    
            if bluetooth.isscanning():
                bluetooth.stop_scan()
    
            try:
    
                connection = bluetooth.connect(adv.mac)
    
                while not connection.isconnected():
                    pass
    
                if connection.isconnected():
    
                    print("Connected to device with addr = {}".format( binascii.hexlify( adv.mac ) ) )
                    print("and name = ", bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL))
                    break
    
            except:
                # start scanning again
                bluetooth.start_scan(-1)
                continue
    
    print('test1')
    services = connection.services()
    print('test2')


  • @nagarjuna-reddy
    you removed it before loop but better here if you remove it from loop
    only connect before (you have only one disconnect)



  • @livius

    hi livius iam checking what you said that but not working so please help me
    program

    def ble_reciever(self):

        bluetooth = Bluetooth()
        print("Scanning 4509 client")
        bluetooth.start_scan(-1)
    
        while True:
            adv = bluetooth.get_adv()
            #if adv:
                #print("Found adv in 4509 client")
                # try to get the complete name
                #print(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL))
    
                # try to get the manufacturer data (Apple's iBeacon data is sent here)
                #print(binascii.hexlify(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_MANUFACTURER_DATA)))
    
    
            if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'LoPy':
                print("connecting rr Lopy in 4509 client")
    
                print("connected Lopy in 4509 client")
                while True:
                    conn = bluetooth.connect(adv.mac)
                    services = conn.services()
                    
                    for service in services:
                        #time.sleep(3)
                        print("start reading..")
                        if type(service.uuid()) == bytes:
                            print('Reading chars from service = {}'.format(service.uuid()))
                        else:
                            print('Reading chars from service = %x' % service.uuid())
                        chars = service.characteristics()
                        for char in chars:
                            if (char.properties() & Bluetooth.PROP_READ):
                                print('char {} value = {}'.format(char.uuid(), char.read()))
                conn.disconnect()
                print("Disconnected 4509 client")
                break
            else:
                time.sleep(0.050)
    
    def ble_transmitter(self):
    
        print('Advertising Bluetooth...')
        bluetooth = Bluetooth()
        bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890abcdef')
    
        def conn_cb (bt_o):
            events = bt_o.events()
            if  events & Bluetooth.CLIENT_CONNECTED:
                print("Client connected")
            elif events & Bluetooth.CLIENT_DISCONNECTED:
                print("Client disconnected")
    
        bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
        bluetooth.advertise(True)
    
        #Create service and characteristic
        srv1 = bluetooth.service(uuid='0000000000000000', isprimary=True)
        chr1 = srv1.characteristic(uuid='0000000000000002', properties=Bluetooth.PROP_INDICATE | Bluetooth.PROP_BROADCAST | Bluetooth.PROP_READ | Bluetooth.PROP_NOTIFY, value='InitialValue')
    
        #Create callback for read events on the characteristic
        def char1_cb(chr):
            events = chr.events()
            if events & Bluetooth.CHAR_READ_EVENT :
                print('Bluetooth.CHAR_READ_EVENT')
    
        chr1.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char1_cb)
    
        #Update the characteristic value very few seconds
        #i=0
        while True:
    
            val = 'Value{}'.format(i)
            print('Setting value: {}'.format(val))
            chr1.value(val) #Set the characteristic value - should trigger notification is a client has registered for notifications
            time.sleep(3)
            i+=1


  • @nagarjuna-reddy
    services = conn.services()
    print("get service......") why its not printing



  • @livius
    i am also check that no working



  • @nagarjuna-reddy said in Bluetooth services is not started:

    conn = bluetooth.connect(adv.mac)
    print("connectede w Lopy in 4509 client")
    while True:
    print("getrdasdf servicess..")
    conn = bluetooth.connect(adv.mac)

    why you connect twice?
    First before loop next inside loop?


 

Hello World?

Pylife on Kickstarter - November 2018








Back Us On Kickstarter >

Pycom on Twitter