This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Android-BLE-Library development

Hi:

    We has a device use the hid keyboard and privacy gatt Service. Now we development the app with Android-BLE-Library, and  can not to resolve some question.

    1.      While get the remote device name with call BluetoothDevice.getName(),  the library return the short name but not the full name.

    2.     How to enable the hid keyboard service by default? while connected to ios is enable auto.

Thanks!

 Sam

  • Hi,

    Indeed, iOS reads the Device Name characteristic on it's own after connection. Android, I think, does not. Instead, it exposes Generic Access service to the user (iOS does not), so you may read this characteristic after connection on your own, or often also write a value to change the name. We don't recommend using BluetoothDevice.getName() as it's using a cached name. For advertising packets is always better to use a name from the advertising data: https://developer.android.com/reference/android/bluetooth/le/ScanRecord.html -> getName().

    Regarding the second issue, when a HID device is bonded it should be enabled automatically. HID service is available form Android 4.4 onward. Perhaps you may have to initiate bonding from Android Settings -> [Connected Devices ->] Bluetooth.

    Hope that helped,

    Aleksander

  • Hi, Aleksander:

        The first issue is solve already.

        Regarding the second issue, just as you said HID device is enabled automatically if initiate bonding from android Setting by manual.
         But, if we use the Android-Scanner-Compat-Library and Android-BLE-Library to initial bonding, the HID device is not enabled automatically.
        Can we do it by software, just like iPhone enabled it automatically after nRF52832 is bonded with android phone? And how to do?
    Thanks!
    Sam
  • If it doesn't work automatically, there is no API you could use to enable it. Are you calling `createBond()`method, or trying to read a protected characteristic? None of this methods is working?

  • Hi, Aleksander:

        I am try the HID device enable automatically.

        And , another question, the BroadcastReceiver is not work any more after set the Service to Remote .

        Can i use the BroadcastReceiver in here?

    Thanks!

    Sam

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:dist="http://schemas.android.com/apk/distribution"
        package="com.hyperspace.smartwatch">
    
        <uses-permission android:name="android.permission.BLUETOOTH" />
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <service
                android:name=".service.BluetoothManageService"
                android:process=":Remote"
                android:enabled="true"
                android:exported="true"/>
    
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    
    public class BluetoothManageService extends Service implements BleManagerCallbacks{
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            IntentFilter intentFilter = new IntentFilter();
    		intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
            registerReceiver(notificationReceive, intentFilter);
        }
    
        public final BroadcastReceiver NotificationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(final Context context, final Intent intent) {
                String action = intent.getAction();
    
                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
                    final int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);
    
                    switch (state) {
                        case BluetoothAdapter.STATE_ON:
                            // On older phones (tested on Nexus 4 with Android 5.0.1) the Bluetooth requires some time
                            // after it has been enabled before some operations can start. Starting the GATT server here
                            // without a delay is very likely to cause a DeadObjectException from BluetoothManager#openGattServer(...).
                            break;
    
                        case BluetoothAdapter.STATE_TURNING_OFF:
                        case BluetoothAdapter.STATE_OFF:
    
                            break;
                    }
                }
            }
        };
    }

  • I think you can use the service with a broadcast receiver on such service, but remember that using services run ning in another thread is very tricky. You can't bind to them, you have to send data using handlers and messages, like between 2 different apps. Are you sure your service is created and started? Also, a service running on background may be killed quite quickly on newer Android versions. Check out background thread executions on Android.

Related