尝试通过BLE连接时弹出蓝牙配对

时间:2017-12-13 06:39:26

标签: android bluetooth bluetooth-lowenergy

我们有一个Android设备,既可以作为蓝牙同步,也可以作为蓝牙外设。 当Android / IOS设备尝试通过BLE连接我的蓝牙外设时,有时会在Android / IOS设备上弹出蓝牙配对。

蓝牙同步已经从系统级完成,这里是ble外设的代码。

public final class ClusterPeripheral {
private static final String TAG = ClusterPeripheral.class.getSimpleName();

private static final UUID ClusterDevice_Service = UUID.fromString("0095aad6-8719-11e7-bb31-be2e44b06b34");

private static final UUID Cluster_PACKET_WRITE_CHARACTERSTICS = UUID.fromString("1d090324-871a-11e7-bb31-be2e44b06b34");

private static final UUID Cluster_BIKE_KEY_CHARACTERISTICS = UUID.fromString("21a25f42-a1ab-11e7-abc4-cec278b6b50a");

private static final UUID Cluster_CONNECTION_STATE_CHARACTERISTICS = UUID.fromString("4a32defa-a8b8-11e7-abc4-cec278b6b50a");

private static final UUID Cluster_NAVIGATION_STATE_CHARACTERISTICS = UUID.fromString("4a8cc23a-c13b-11e7-abc4-cec278b6b50a");

public static final UUID Cluster_BIKE_KEY_CHANGE_CHARACTERISTICS = UUID.fromString("6a32e314-a8b8-11e7-abc4-cec278b6b50a");

public static final UUID Cluster_CONNECTION_CHARACTERISTICS = UUID.fromString("d13674a6-ace2-11e7-abc4-cec278b6b50a");


public static final UUID Cluster_IOS_NOTIFICATION_CHARACTERISTICS = UUID.fromString("46e55460-cdea-11e7-abc4-cec278b6b50a");

public static final UUID CLIENT_CHARACTERISTIC_CONFIGURATION_UUID = UUID
        .fromString("00002902-0000-1000-8000-00805f9b34fb");

private static final UUID CHARACTERISTIC_USER_DESCRIPTION_UUID = UUID
        .fromString("00002901-0000-1000-8000-00805f9b34fb");
private AppPreferences appPreferences;

private BluetoothGattService Cluster_Service_Instance;
public static BluetoothGattCharacteristic Cluster_WRITE_InstanceCharacterstics;
public static BluetoothGattCharacteristic Cluster_BIKE_KEY_InstanceCharacterstics;
public static BluetoothGattCharacteristic Cluster_STATE_CONNECTED_Characterstics;
public static BluetoothGattCharacteristic Cluster_IOS_NOTIFICATION_Characterstics;

public static BluetoothGattCharacteristic Cluster_Connection_Characteristics;
public static BluetoothGattCharacteristic Cluster_NAVIGATION_STATE_Characterstics;

public static BluetoothGattCharacteristic Cluster_BIKE_KEY_CHANGE_Characterstics;
public ClusterPeripheral() {

    appPreferences = AppPreferences.getInstance(Cluster.mActivity);
    Cluster_WRITE_InstanceCharacterstics =
            new BluetoothGattCharacteristic(Cluster_PACKET_WRITE_CHARACTERSTICS,
                    BluetoothGattCharacteristic.PROPERTY_WRITE,
                    BluetoothGattCharacteristic.PERMISSION_WRITE);

    //initializing the bike key characteristic
    Cluster_BIKE_KEY_InstanceCharacterstics =
            new BluetoothGattCharacteristic(Cluster_BIKE_KEY_CHARACTERISTICS,
                    BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
                    BluetoothGattCharacteristic.PERMISSION_READ);

    //adding descriptor to the bike key characteristic
    Cluster_BIKE_KEY_InstanceCharacterstics.addDescriptor(
            getClientCharacteristicConfigurationDescriptor());

    //initializing the state connected characteristic
    Cluster_STATE_CONNECTED_Characterstics = new BluetoothGattCharacteristic(Cluster_CONNECTION_STATE_CHARACTERISTICS,
            BluetoothGattCharacteristic.PROPERTY_READ,
            BluetoothGattCharacteristic.PERMISSION_READ);

    //initializing the navigation state characteristic
    Cluster_NAVIGATION_STATE_Characterstics = new BluetoothGattCharacteristic(Cluster_NAVIGATION_STATE_CHARACTERISTICS,
            BluetoothGattCharacteristic.PROPERTY_READ,
            BluetoothGattCharacteristic.PERMISSION_READ);


    Cluster_Connection_Characteristics = new BluetoothGattCharacteristic(Cluster_CONNECTION_CHARACTERISTICS,
            BluetoothGattCharacteristic.PROPERTY_WRITE,

            BluetoothGattCharacteristic.PERMISSION_WRITE);
    //initilizing the bluetooth gatt service
    Cluster_Service_Instance = new BluetoothGattService(ClusterDevice_Service,
            BluetoothGattService.SERVICE_TYPE_PRIMARY);

    Cluster_BIKE_KEY_CHANGE_Characterstics = new BluetoothGattCharacteristic(Cluster_BIKE_KEY_CHANGE_CHARACTERISTICS,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY);

    Cluster_BIKE_KEY_CHANGE_Characterstics.addDescriptor(
            getClientCharacteristicConfigurationDescriptor());

    Cluster_BIKE_KEY_CHANGE_Characterstics.addDescriptor(getCharacteristicUserDescriptionDescriptor("Update Vehichle Key"));

    Cluster_IOS_NOTIFICATION_Characterstics = new BluetoothGattCharacteristic(Cluster_IOS_NOTIFICATION_CHARACTERISTICS,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY);

    Cluster_IOS_NOTIFICATION_Characterstics.addDescriptor(
            getClientCharacteristicConfigurationDescriptor());





    //adding the charateristics to the service
    Cluster_Service_Instance.addCharacteristic(Cluster_WRITE_InstanceCharacterstics);
    Cluster_Service_Instance.addCharacteristic(Cluster_BIKE_KEY_InstanceCharacterstics);
    Cluster_Service_Instance.addCharacteristic(Cluster_STATE_CONNECTED_Characterstics);
    Cluster_Service_Instance.addCharacteristic(Cluster_BIKE_KEY_CHANGE_Characterstics);
    Cluster_Service_Instance.addCharacteristic(Cluster_Connection_Characteristics);
    Cluster_Service_Instance.addCharacteristic(Cluster_NAVIGATION_STATE_Characterstics);
    Cluster_Service_Instance.addCharacteristic(Cluster_IOS_NOTIFICATION_Characterstics);


    Cluster_IOS_NOTIFICATION_Characterstics.setValue(0,BluetoothGattCharacteristic.FORMAT_UINT16, 0);

    if (appPreferences.getPassKey().length() != 0)
        Cluster_BIKE_KEY_InstanceCharacterstics.setValue(Integer.parseInt(appPreferences.getPassKey()),
                BluetoothGattCharacteristic.FORMAT_UINT16, 0);

    //setting the value of charateristic to Zero, if there is no device connected and 1, if a device is connected.
    Cluster_STATE_CONNECTED_Characterstics.setValue(0,
            BluetoothGattCharacteristic.FORMAT_UINT16, 0);

    //setting the value of charateristic to Zero, if the navigation screen is not open
    Cluster_NAVIGATION_STATE_Characterstics.setValue(0,
            BluetoothGattCharacteristic.FORMAT_UINT16, 0);

    //setting default value of bike pass key
    if (appPreferences.getPassKey().length() != 0)
        Cluster_BIKE_KEY_CHANGE_Characterstics.setValue(Integer.parseInt(appPreferences.getPassKey()),
                BluetoothGattCharacteristic.FORMAT_UINT16, 0);
}

/**
 * Return the instance of Cluster service.
 *
 * @return
 */
public BluetoothGattService getBluetoothGattService() {

    AndroidAppUtils.showLog(TAG, "*****************getBluetoothGattService called");
    return Cluster_Service_Instance;
}


/**
 * Return UUID of Cluster Service
 *
 * @return
 */
public ParcelUuid getServiceUUID() {
    AndroidAppUtils.showLog(TAG, "*****************getServiceUUID called");

    return new ParcelUuid(ClusterDevice_Service);
}


/**
 * Function to communicate to the ServiceHelper that a device wants to write to a
 * characteristic.
 * <p>
 * The ServiceHelper should check that the value being written is valid and
 * return a code appropriately. The ServiceHelper should update the UI to reflect the change.
 *
 * @param characteristic Characteristic to write to
 * @param value          Value to write to the characteristic
 * @return {@link BluetoothGatt#GATT_SUCCESS} if the read operation
 * was completed successfully. See {@link BluetoothGatt} for GATT return codes.
 */
public int writeCharacteristic(BluetoothGattCharacteristic characteristic, int offset, byte[] value, final String writtendata) {

    AndroidAppUtils.showLog(TAG, "Length of data recieved: " + value.length + writtendata);
    if (offset != 0) {
        return BluetoothGatt.GATT_INVALID_OFFSET;
    }
    // Heart Rate control point is a 8bit characteristic
    if (value.length != 1) {
        Log.d(TAG, "value.length:" + BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH);
        return BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
    }

    return BluetoothGatt.GATT_SUCCESS;
}


public static BluetoothGattDescriptor getClientCharacteristicConfigurationDescriptor() {
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(
            CLIENT_CHARACTERISTIC_CONFIGURATION_UUID,
            (BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE));
    descriptor.setValue(new byte[]{0, 0});
    return descriptor;
}

public static BluetoothGattDescriptor getCharacteristicUserDescriptionDescriptor(String defaultValue) {
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(
            CHARACTERISTIC_USER_DESCRIPTION_UUID,
            (BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE));
    try {
        descriptor.setValue(defaultValue.getBytes("UTF-8"));
    } finally {
        return descriptor;
    }
}

}

0 个答案:

没有答案