android - How do you get data from a Bluetooth LE device -


i have bluetooth barcode scanner supports bluetooth le , trying barcode information when 1 scanned.

i can connect fine onservicesdiscovered gets called in bluetoothgattcallback not sure there.

with classic bluetooth connection inputstream bluetoothsocket , wait read() give data not sure how works bluetooth le. tried looping through bluetoothgattcharacteristic's checking property , if read property call gatt.readcharacteristic(characteristic); gives me useless information , that's before attempt scan something.

so how barcode data scanner?

this scanner have https://www.zebra.com/us/en/support-downloads/scanners/ultra-rugged-scanners/li3608-li3678.html

the data provided ble devices called characteristics. these data packages specially formed, tightly packed byte arrays encode specific values specific services. can check out services @ official bluetooth website. here you'll find defined (authoritative) gatt services , belonging characteristics.

for example, have ble cycling computer reports speed , cadence. cycling speed , cadence item in list. entry contains uuid (0x1816) of service , link data sheet contains characteristics. if go service characteristics table, you'll find couple entries. want speed , cadence, you'll open csc measurement (the type field of entry) takes characteristic's data sheet. here you'll see value fields table defines specific values can read characteristic.

this bluetooth le part in general, android. note, you'll have these fields in order values characteristics. i'm gonna assume have characteristic want data from. here's quick sample retrieves wheel , crank revolutions (if available).

bluetoothgattcharacteristic characteristic = ... ;  int offset = 0; // define offset used when reading next field  // format_* values constants in bluetoothgattcharacteristic // these represent values can find in "value fields" table in "format" column int flags = characteristic.getintvalue(format_uint8, offset);  offset += 1; // uint8 = 8 bits = 1 byte  // have check flags' 0th bit see if c1 field exists  if ((flags & 1) != 0) {     int cumulativewheelrevolutions = characteristic.getintvalue(format_uint32, offset);     offset += 4; // uint32 = 32 bits = 4 bytes      int lastwheeleventtime = characteristic.getintvalue(format_uint16, offset);     offset += 2; // uint16 = 16 bits = 2 bytes }  // have check flags' 1st bit see if c2 field exists  if ((flags & 2) != 0) {     int cumulativecrankrevolutions = characteristic.getintvalue(format_uint16, offset);     offset += 2;      int lastcrankeventtime = characteristic.getintvalue(format_uint16, offset);     offset += 2; } 

the flags field needs checked specific bits because possible device not report every type of data, e.g. doesn't count wheel revolutions. selected characteristic's sheet contains relevant information field (if exists).

it's worth noting documentation says that

the csc measurement characteristic (csc refers cycling speed , cadence) variable length structure containing flags field and, based on contents of flags field, may contain 1 or more additional fields [...]

this why cannot assume cumulative crank revolutions value found @ 7 bytes (8 + 32 + 16 bits; 1 + 4 + 2 bytes respectively) offset , offset should counted progress along fields.


this example reading cycling speed , cadence values ble device. you'll have these available fields , values every device (or rather service) want support in application. if device special 1 , cannot found in gatt directory, you'll need consult device's manual, sdk, or vendor more info.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -