tjdam007 / BleConnect

How Connect and auto Reconnect with BLE DEVICES.
1 stars 0 forks source link

It did not work in Android 5.0.1 #1

Open mjohn123 opened 8 years ago

mjohn123 commented 8 years ago

Thank you for sharing an excellent job. I tested it on my phone (Galaxy S4, Android 5.0.1) but it did not work. The alert function works when I turn on/turn off Bluetooth on my phone. However, when I connect the phone with my smart BLE, the status still on Disconnected. Besides, I restart my phone; the app does not reconnect the smart BLE (paired before). Thanks

tjdam007 commented 8 years ago

Thanks mjohn123 for notifying the issue, I will look into it

mjohn123 commented 8 years ago

Finally, I solved the issue. The issue is from line 65 and 80 in class BleScanService.java. It checked your device name. You set default your device name as bluetoothDevice.getName().equals("SAFER")) In my case, it has another name. So, your code does not pass the condition. To modify it, I suggest that you can store the device name and device address at the first time you check, then you can use it with a dynamic name, instead of fixed name. It only remains a minor issue about connection: The first scenario assumes that the BLE device and Phone are connected. Then, I bring the phone far away the BLE to make a disconnection between these devices. Then I bring the phone is close the BLE (within the range of BLE), the app does not reconnect the BLE. If I want to connect, I must press the configuration button (having a sound). I am using CSR Light device. The second scenario: Two devices are connected. Then I restart my phone, sometimes it can reconnect again but almost time they did not connect. The message in Logcat is

-10 21:23:34.901 6092-6129/com.dev4solutions.bleconnect:reconnectProcess D/BLEIsConnect: Passed getBluetoothAdapter enable
09-10 21:23:34.901 6092-6129/com.dev4solutions.bleconnect:reconnectProcess D/BluetoothManager: getConnectionState()
09-10 21:23:34.901 6092-6129/com.dev4solutions.bleconnect:reconnectProcess D/BluetoothManager: getConnectedDevices
09-10 21:23:35.011 10559-10559/com.dev4solutions.bleconnect:scanService D/ResourcesManager: creating new AssetManager and set to /data/app/com.dev4solutions.bleconnect-1/base.apk
09-10 21:23:35.091 10559-10559/com.dev4solutions.bleconnect:scanService D/BleScanService: onCreate
09-10 21:23:35.191 10559-10569/com.dev4solutions.bleconnect:scanService D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=6

Finally, Could you suggest to me which class can I write code to perform read and write Characteristic (data) to communicate with BLE device as BluetoothLeGatt.java sample?

Thank you for your good job

tjdam007 commented 8 years ago

Thanks mjohn123 for the solution and you can write your code in inner class BleConnectService .BluetoothGattCallbackImpl

mjohn123 commented 8 years ago

Thanks tjdam007 for your guidance. I inserted the onCharacteristicRead, onCharacteristicChanged and my own readCustomCharacteristic in the class BleConnectService. As I understood, the main activity is in the class BleStatusActivity (as DeviceControlActivity in the sample code). Hence, I want to call my readCustomCharacteristic from the BleStatusActivity class. However, it was wrong. The data does not print. I think the connectServiceBLE is null. This is my code to call the function readCustomCharacteristic in the `BleStatusActivity.'

    BleConnectService connectServiceBLE;
    private final ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            connectServiceBLE = ((BleConnectService.LocalBinder) service).getService();

            // Automatically connects to the device upon successful start-up initialization.
            //connectServiceBLE.connect(mDeviceAddress);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            connectServiceBLE = null;
        }
    };
    public void onClickRead(View v){
        if(connectServiceBLE != null) {
            connectServiceBLE .readCustomCharacteristic();
        }
    }

In onResume()

intentFilter.addAction(BleUtils.ACTION_DATA_AVAILABLE);

And on onReceive of BroadcastReceiver

case BleUtils.ACTION_DATA_AVAILABLE:
                    //displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
                    Log.d("DATA", "ACTION_DATA_AVAILABLE"+String.valueOf(intent.getStringExtra(BleUtils.EXTRA_DATA)));

And in the BlueConnectService class is

    public class LocalBinder extends Binder {
        BleConnectService getService() {
            return BleConnectService.this;
        }
    }