3

I try to make two bluetooth BLE apps:

  • one central role
  • one peripherall

But I have found some problem and my apps crashed.

This is the step which I followed:

1- client set the descriptor

BluetoothGattDescriptor descriptor = MeasurementCharacteristic.getDescriptor(
                UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        bluetoothGatt.writeDescriptor(descriptor); 

2 - The server in onDescriptorWriteRequest set the

MeasurementCharacteristic.setValue(test.getBytes());
gattserver.notifyCharacteristicChanged(device, MeasurementCharacteristic, false);

3 - The client receive this notification and I have done this :

CommandsCharacteristic.setValue(string_value.getBytes());
                write(CommandsCharacteristic);

4 - The server receives this in onCharacteristicWriteRequest and I have done this :

if(uuid.equals(CostantUUid.Commands))
            {
                .....
                engineMeasurementCharacteristic.setValue(test.getBytes());
                ble.getGattServer().notifyCharacteristicChanged(device, MeasurementCharacteristic, false);
            }

5- on the client the notifyCharacteristicChanged will be called and we will return to step 3

But sadly the comunication automatically shut down after few message.

  • client enable descriptor
  • server send notification to the client
  • client read the new characteristic and re-write to server
  • server resend notify to client
  • client read send message to client
  • the message doesn't arrive...

With the log I have seen that onCharacteristicWrite's function is been called only once in all the app life. Is been called after the message that it doesn't arrive with status 133 , but I don't understand why it not been called after the other messages.

@Override
                public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,int status)
                {

                    super.onCharacteristicWrite(gatt, characteristic, status);

                    if(status==BluetoothGatt.GATT_SUCCESS)
                    {
                        Log.i("test","GATT SUCCESS");
                    }
                    if(status==BluetoothGatt.GATT_CONNECTION_CONGESTED)
                    {
                        Log.i("test","GATT WRITE connection congested");
                    }
                    if(status==BluetoothGatt.GATT_WRITE_NOT_PERMITTED)
                    {
                        Log.i("test","GATT WRITE not permitted");
                    }
                    if(status==BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH)
                    {
                        Log.i("test","GATT invalid attribute lenght");
                    }
                    if(status==BluetoothGatt.GATT_FAILURE)
                    {
                        Log.i("test","GATT WRITE other errors");
                    }
                    if(status==BluetoothGatt.GATT_CONNECTION_CONGESTED)
                    {
                        Log.i("test","GATT WRITE congested");
                    }
                    if(status==BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION)
                    {
                        Log.i("test","GATT WRITE authentication");
                    }
                    else
                    {
                        Log.i("test","GATT WRITE :"+status);
                    }
                }

central role's log:

    03-30 15:09:22.308: W/BLE(30615): STATE_CONNECTED
*******
*******
03-30 15:09:50.958: D/HeadsetStateMachine(2312): Disconnected process message: 10
03-30 15:09:50.958: D/KeyguardUpdateMonitor(1026): received broadcast android.intent.action.BATTERY_CHANGED
03-30 15:09:50.958: D/KeyguardUpdateMonitor(1026): handleBatteryUpdate
03-30 15:09:50.958: D/BatteryService(795): level:64, scale:100, status:3, health:2, present:true, voltage: 3898, temperature: 283, technology: Li-ion, AC powered:false, USB powered:false, Wireless powered:false, icon:17303543, invalid charger:0, online:1, current avg:-242, charge type:0, power sharing:false
03-30 15:09:50.958: D/BatteryService(795): Sending ACTION_BATTERY_CHANGED.
03-30 15:09:50.958: D/STATUSBAR-PhoneStatusBar(1026):  mBrightnessEnablebySettings = true mBrightnessEnablebyBattery = true mBrightnessEnablebyDisableFlag = true mPmsBrightnessEnablebySettings = true
03-30 15:09:50.958: D/BatteryMeterView(1026): ACTION_BATTERY_CHANGED : level:64 status:3 health:2
03-30 15:09:50.968: D/STATUSBAR-IconMerger(1026): checkOverflow(2112), More:false, Req:false Child:2
03-30 15:09:52.978: D/SensorService(795):   0.0 -0.1 9.9
03-30 15:09:55.158: D/bt_vendor(2312): op for 7
03-30 15:09:55.158: D/bt_upio(2312): proc btwrite assertion
03-30 15:09:55.178: D/SSRMv2:SIOP(795): SIOP:: AP = 300, Delta = 0
03-30 15:09:55.188: E/bt-btm(2312): btm_sec_disconnected - Clearing Pending flag
03-30 15:09:55.188: D/IOP_DB_BT(2312): db_query_create: id DisablePwlCtrReq :: key KEY_BDADDR, value 4b:87:e8:45:76:b9
03-30 15:09:55.188: D/IOP_DB_BT(2312): db_query_add_key: key KEY_LMP_MFCT, value 0
03-30 15:09:55.188: D/IOP_DB_BT(2312): db_query_add_key: key KEY_LMP_VER, value 0:0
03-30 15:09:55.188: D/IOP_DB_BT(2312): db_query_add_key: key KEY_DIR_ALL, value *
03-30 15:09:55.188: D/IOP_DB_BT(2312): db_query_execute: result 1
03-30 15:09:55.188: D/BtGatt.btif(2312): btif_gattc_upstreams_evt: Event 4
03-30 15:09:55.188: D/BtGatt.GattService(2312): onWriteCharacteristic() - address=4B:87:E8:45:76:B9, status=133
03-30 15:09:55.188: D/BluetoothGatt(30615): onCharacteristicWrite() - Device=4B:87:E8:45:76:B9 UUID=687bf0f0-d6dc-11e4-b9d6-1681e6b88ec1 Status=133
03-30 15:09:55.188: D/BtGatt.btif(2312): btif_gattc_upstreams_evt: Event 5
03-30 15:09:55.188: D/BtGatt.GattService(2312): onDisconnected() - clientIf=6, connId=6, address=4B:87:E8:45:76:B9
03-30 15:09:55.188: D/BluetoothGatt(30615): onClientConnectionState() - status=0 clientIf=6 device=4B:87:E8:45:76:B9
03-30 15:09:55.188: I/*****(30615): status = 0 newState = 0
03-30 15:09:55.188: W/BLE(30615): STATE_DISCONNECTED

Peripheral log:

03-30 15:09:24.001: D/MyActivity(10354): onCharacteristicReadRequest requestId=3 offset=00002a24-0000-1000-8000-00805f9b34fb
03-30 15:09:24.003: D/BtGatt.btif(9706): btif_gatts_send_response
03-30 15:09:24.003: D/BtGatt.btif(9706): btgatts_handle_event: Event 2012
03-30 15:09:24.003: D/BtGatt.GattService(9706): onResponseSendCompleted() handle=46
03-30 15:09:24.096: D/BtGatt.btif(9706): btapp_gatts_handle_cback: Event 1
03-30 15:09:24.097: D/BtGatt.GattService(9706): onAttributeRead() UUID=00002a27-0000-1000-8000-00805f9b34fb, serverIf=5, type=2
03-30 15:09:24.097: D/MyActivity(10354): onCharacteristicReadRequest requestId=4 offset=00002a27-0000-1000-8000-00805f9b34fb
03-30 15:09:24.098: D/BtGatt.btif(9706): btif_gatts_send_response
03-30 15:09:24.098: D/BtGatt.btif(9706): btgatts_handle_event: Event 2012
03-30 15:09:24.098: D/BtGatt.GattService(9706): onResponseSendCompleted() handle=48
03-30 15:09:24.243: D/BtGatt.btif(9706): btapp_gatts_handle_cback: Event 2
03-30 15:09:24.243: D/BtGatt.GattService(9706): onAttributeWrite() UUID=00002902-0000-1000-8000-00805f9b34fb, serverIf=5, type=3
03-30 15:09:24.246: D/BtGatt.btif(9706): btif_gatts_send_response
03-30 15:09:24.246: D/BtGatt.btif(9706): btgatts_handle_event: Event 2012
03-30 15:09:24.246: D/BtGatt.GattService(9706): onResponseSendCompleted() handle=52
03-30 15:09:24.249: D/BtGatt.btif(9706): btif_gatts_send_indication
03-30 15:09:24.252: D/BtGatt.btif(9706): btgatts_handle_event: Event 2011
03-30 15:09:24.257: D/BtGatt.btif(9706): btapp_gatts_handle_cback: Event 5
03-30 15:09:24.257: D/BtGatt.GattService(9706): onNotificationSent() connId=5, status=0
03-30 15:09:24.341: D/BtGatt.btif(9706): btapp_gatts_handle_cback: Event 2
03-30 15:09:24.341: D/BtGatt.GattService(9706): onAttributeWrite() UUID=687bf0f0-d6dc-11e4-b9d6-1681e6b88ec1, serverIf=5, type=2
03-30 15:09:24.342: D/MyActivity(10354): onCharacteristicWriteRequest requestId=6 offset=687bf0f0-d6dc-11e4-b9d6-1681e6b88ec1
03-30 15:09:24.343: I/test(10354): Message received ciao 
03-30 15:09:24.344: D/BtGatt.btif(9706): btif_gatts_send_indication
03-30 15:09:24.345: D/BtGatt.btif(9706): btgatts_handle_event: Event 2011
03-30 15:09:24.345: D/BtGatt.btif(9706): btapp_gatts_handle_cback: Event 5
03-30 15:09:24.345: D/BtGatt.GattService(9706): onNotificationSent() connId=5, status=0
03-30 15:09:54.518: W/bt-btif(9706): bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0013
03-30 15:09:54.518: W/bt-btif(9706): bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0013
03-30 15:09:54.519: W/bt-btif(9706): bta_gattc_conn_cback() - cif=6 connected=0 conn_id=6 reason=0x0013
03-30 15:09:54.519: E/bt-btm(9706): btm_sec_disconnected - Clearing Pending flag
03-30 15:09:54.519: D/BtGatt.btif(9706): btapp_gatts_handle_cback: Event 15
03-30 15:09:54.519: D/BtGatt.GattService(9706): onConnected() connId=5, address=B8:6C:E8:5A:99:D2, connected=false
03-30 15:09:54.520: E/BluetoothRemoteDevices(9706): aclStateChangeCallback: Device is NULL
03-30 15:09:54.521: D/BluetoothGattServer(10354): onServerConnectionState() - status=0 serverIf=5 device=B8:6C:E8:5A:99:D2
03-30 15:09:54.522: D/MyActivity(10354): onConnectionStateChange status=0->0
03-30 15:09:59.001: E/WifiStateMachine(449): WifiStateMachine CMD_START_SCAN source -2 txSuccessRate=0.69 rxSuccessRate=23.35 targetRoamBSSID=any RSSI=-69
03-30 15:10:02.207: D/HeadsetStateMachine(9706): Disconnected process message: 10, size: 0
03-30 15:10:19.001: E/WifiStateMachine(449): WifiStateMachine CMD_START_SCAN source -2 txSuccessRate=0.78 rxSuccessRate=22.32 targetRoamBSSID=any RSSI=-68
03-30 15:10:38.999: E/WifiStateMachine(449): WifiStateMachine CMD_START_SCAN source -2 txSuccessRate=0.32 rxSuccessRate=34.32 targetRoamBSSID=any RSSI=-67

EDIT : I have cleaned the precedent message with the add of all of your suggestion/advice :)

EDIT 2 : The peripherall still alive, but disconnected. If I reconnect the connection restart, and redo the same step above :(

EDIT3 : on the peripherall I have setted in this way :

settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
    settingsBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
    settingsBuilder.setConnectable(true);

EDIT5 : I solve my problem. There was an error inside OnCharacteristicWriteRequest inside peripherall. The app still working but the connection was crashing. :)

EDIT6 : How can retrieve the real MAC address of my peripherall ? Because on the client side when I use "getAddress()" the MAC address changes each time the peripherall reboots

aeroxr1
  • 1,014
  • 1
  • 14
  • 36
  • 1
    you do not call connect on server(peripheral) side, you will call connect from monitor(other phone) role. Server role should just register services, and listen for commands from Monitor. – AAnkit Mar 25 '15 at 07:13
  • Perfect..i ask because bluethootgattsrver's connect method makes me some doubt. However i can't understand why the first write from monitor and the second from peripheral works well, while the third from client doesnt arrive to the monitor .. – aeroxr1 Mar 25 '15 at 07:24
  • 1
    bluethootgattsrver's connect, this will be called by peripheral in case of autoconnection feature(like proximity profile). for your another question, it would require logs from both side to understand what is happening in third attempt. – AAnkit Mar 25 '15 at 08:11
  • There aren't any error logs , because there aren't error :( Another thing for verify that I have understand : if on "client" I call connect with auto connection's feature on the other side I have to implement the bluethoothgattserver's connect ? :) – aeroxr1 Mar 25 '15 at 09:33
  • Or better still there are error But the app still alive without gives error logs. public boolean connect (BluetoothDevice device, boolean autoConnect) : i don't really understand how to use this function. – aeroxr1 Mar 25 '15 at 09:51
  • 1
    you have to call it with device(remote device), you can create a device instance via its address. There may be errors in bluetooth Service or c code, which wont show in your app logs, you need to take logs via adb logcat command – AAnkit Mar 25 '15 at 11:08
  • What do you mean with remote device ? peripherall or central role ? :D I have watched the logcat message but I didn't find anything. I recheck ! – aeroxr1 Mar 25 '15 at 11:22
  • 1
    remote/other device, if you writing the code on client then remote is server and vice versa, you can create device instance by BluetoothAdapter.getBlu..Device(address) – AAnkit Mar 25 '15 at 11:41
  • I add the log to the first post :) Thanks for everything :) – aeroxr1 Mar 25 '15 at 13:44
  • 1
    in the central role logs, there is error on writeRequest(see status=133), which leads to disconnection. see another line after that is "onDisconnected". – AAnkit Mar 25 '15 at 14:49
  • yes, but I don't understand why :/ – aeroxr1 Mar 25 '15 at 15:09
  • 1
    oh, I see you mentioned this already in the question. Are you sure that you are not calling disconnect? I think your code in the onWrite calls disconnect due to its failure. Show more code of client/monitor/ side. My reply would be after 12 hours now. – AAnkit Mar 25 '15 at 17:22
  • I have edited my first post. I have rewrite all post with the new information. I hope could you give me help :) – aeroxr1 Mar 30 '15 at 12:36
  • 1
    where is crash logs? – AAnkit Mar 30 '15 at 12:41
  • @AAnkit I add all the log of central role app and peripherall, the problem seem affect different users. In particular I have noticed this log line : 03-30 15:09:54.519: E/bt-btm(9706): btm_sec_disconnected - Clearing Pending flag – aeroxr1 Mar 30 '15 at 20:06

1 Answers1

-1

Try out the official example at Bluetooth Low Energy. I've used this example and it works.

In this example, the Android app is the client, but you can have the Android app play the role of the GATT server as well. See BluetoothGattServer for more details.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • 2
    In that example it creates a Central Role Application or I'm wronging ? I need example of a peripheral role application , where is used a bluetoothgattserver :( – aeroxr1 Mar 24 '15 at 15:53
  • I add an answer with other details :) – aeroxr1 Mar 24 '15 at 17:15
  • 2
    @aeroxr1: it would be better if you added those details in your question itself, and delete that "answer" :) – Yash Sampat Mar 24 '15 at 17:17
  • Thanks :) I don't know where I wrong on my code :( One day and I didn't find one soluction... :/ – aeroxr1 Mar 24 '15 at 17:28
  • 1
    @aeroxr1: take a break, and then study that example in more detail. If you have any doubts you can ask me. Meanwhile I'm taking a look at the code you posted :) – Yash Sampat Mar 24 '15 at 17:38
  • my problem seems affect different users .. Do you see any error in what did I do ? – aeroxr1 Mar 30 '15 at 20:01
  • what is the problem exactly ? – Yash Sampat Mar 30 '15 at 20:06
  • Random disconnession after this : "03-30 15:09:54.519: E/bt-btm(9706): btm_sec_disconnected - Clearing Pending flag" And my problem is similar to other user in the comment below.. I have updated the first post with more explanation :) – aeroxr1 Mar 30 '15 at 20:08