我有一个应用程序(Lollipop),它向Recon JET做广告,它将使用自己的应用程序发现它。他们将成功连接,我知道这是因为我有一个调用BluetoothManager.getConnectedDevices(BluetoothProfile.GATT)并返回MAC地址的按钮。虽然它连接,但它不会调用onConnectionStateChange。
以下是我如何向JET做广告:
if(advertiser == null){
shouldAdvertise = true;
}
BluetoothManager bluetoothManager = (BluetoothManager) this.getSystemService(BLUETOOTH_SERVICE);
//Add advertise settings to the advertisement
final AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_BALANCED )
.setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH )
.setConnectable( false )
.build();
//Create a ParcelUUID object out of the Service UUID
ParcelUuid pUuid = new ParcelUuid( ServiceUuid );
//Add advertise data to the advertisement
final AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName( true )
.addServiceUuid( pUuid )
.addServiceData( pUuid, "Data".getBytes( Charset.forName( "UTF-8" ) ) )
.build();
//Create the temperature characteristic that will be on the server
final BluetoothGattCharacteristic btTemp =
new BluetoothGattCharacteristic(TempUuid,
//Read+write permissions
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
//Create the humidity characteristic that will be on the server
final BluetoothGattCharacteristic btHum =
new BluetoothGattCharacteristic(HumUuid,
//Read+write permissions
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
//Create the pressure characteristic that will be on the server
final BluetoothGattCharacteristic btPres =
new BluetoothGattCharacteristic(PresUuid,
//Read+write permissions
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
//Create the adapter from the manager
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
bluetoothAdapter.setName("Data Hub Server");
/**
* The callback for the advertisement. Basically will declare if the
* advertisement succeeded or if it failed.
*/
final AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
Log.d("FATAL","Advertising...");
}
@Override
public void onStartFailure(int errorCode) {
Log.e( "FATAL", "Advertising onStartFailure: " + errorCode );
super.onStartFailure(errorCode);
}
};
//Create the server
BluetoothGattServer server = bluetoothManager.openGattServer(this,mGattCallback);
Toast.makeText(this, server.toString() , Toast.LENGTH_SHORT).show();
//Create the service to hold the characteristics
BluetoothGattService service =new BluetoothGattService(ServiceUuid,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
// Add the characteristics to the service and add the service to
// the server
service.addCharacteristic(btTemp);
service.addCharacteristic(btHum);
service.addCharacteristic(btPres);
boolean serviceAdded = server.addService(service);
Log.d("FATAL","Was the service added? : " + Boolean.toString(serviceAdded));
//Start advertising
if(shouldAdvertise) {
advertiser.startAdvertising(settings, data, advertisingCallback);
shouldAdvertise = false;
}