Unable to find characteristics in iphone applicatoin of the raspberry pi as BLE peripheral

by Apoorv Mishra   Last Updated July 12, 2019 12:25 PM - source

I'm trying to make my raspberry pi a peripheral device. I am having trouble finding characteristics of the peripheral in my IOS application. I found that when I run my peripheral script multiple times, the characteristics are available randomly. Can you help me out with it? I'm using pybleno library for making raspberry pi a peripheral device. You can find the library here

The code I'm using is given below:

from pybleno import *
import sys
import signal
from EchoCharacteristic import *

print('bleno - echo');

bleno = Bleno()

def onStateChange(state):
   print('on -> stateChange: ' + state);

if (state == 'poweredOn'):
   bleno.startAdvertising('echo', ['ec00'])
else:
   bleno.stopAdvertising();

bleno.on('stateChange', onStateChange)

def onAdvertisingStart(error):
    print('on -> advertisingStart: ' + ('error ' + error if error else 'success'));

if not error:
    bleno.setServices([
        BlenoPrimaryService({
            'uuid': 'ec00',
            'characteristics': [ 
                EchoCharacteristic('ec0F')
                ]
        })
    ])
bleno.on('advertisingStart', onAdvertisingStart)

bleno.start()

print ('Hit <ENTER> to disconnect')

if (sys.version_info > (3, 0)):
    input()
else:
    raw_input()

bleno.stopAdvertising()
bleno.disconnect()

print ('terminated.')
sys.exit(1)

And the code for Charactristics is below:

from pybleno import Characteristic
import array
import struct
import sys
import traceback
from builtins import str

class EchoCharacteristic(Characteristic):

def __init__(self, uuid):
    Characteristic.__init__(self, {
        'uuid': uuid,
        'properties': ['read', 'write', 'notify'],
        'value': None
      })

    self._value = array.array('B', [0] * 0)
    self._updateValueCallback = None

def onReadRequest(self, offset, callback):
    print('EchoCharacteristic - %s - onReadRequest: value = %s' % (self['uuid'], [hex(c) for c in self._value]))
    callback(Characteristic.RESULT_SUCCESS, self._value[offset:])

def onWriteRequest(self, data, offset, withoutResponse, callback):
    self._value = data

    print('EchoCharacteristic - %s - onWriteRequest: value = %s' % (self['uuid'], [hex(c) for c in self._value]))

    if self._updateValueCallback:
        print('EchoCharacteristic - onWriteRequest: notifying');

        self._updateValueCallback(self._value)

    callback(Characteristic.RESULT_SUCCESS)

def onSubscribe(self, maxValueSize, updateValueCallback):
    print('EchoCharacteristic - onSubscribe')

    self._updateValueCallback = updateValueCallback

def onUnsubscribe(self):
    print('EchoCharacteristic - onUnsubscribe');

    self._updateValueCallback = None


Related Questions


DIY Eddystone Beacon

Updated February 27, 2017 15:25 PM


BLE scanning time interval for broadcasted data

Updated May 17, 2018 07:25 AM


Bluetooth LE (BLE) pairing behavior

Updated May 21, 2018 03:25 AM