我在Samsung Galaxy Note3(API = 21)上创建BLE外设。代码似乎是合理的,设备被发现(缓慢),但服务无法被发现(使用我的Mac上的LightBlue应用程序进行测试) - 它等待和等待,没有显示任何服务。
这是硬件问题还是我做错了什么?
活动代码:
public class MainActivity extends ActionBarActivity {
private static final String TAG = "BleTest";
public static final String START_PERIPHERAL_GATT_SERVER = "startPeripheralGattServer()";
public static final String FAILED_TO_CREATE_GATT_SERVER = "Failed to create Gatt server";
public static final String START_ADVERTISING = "startAdvertising()";
private BluetoothGattServer mGattServer;
private byte response = 0x55;
public void startPeripheralGattServer() {
Log.d(TAG, START_PERIPHERAL_GATT_SERVER);
Toast.makeText(this, "startPeripheralGattServer()", Toast.LENGTH_SHORT).show();
final BluetoothManager bluetoothManager =
(BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean isMultipleAdvertisementSupported = bluetoothAdapter.isMultipleAdvertisementSupported();
boolean isOffloadedFilteringSupported = bluetoothAdapter.isOffloadedFilteringSupported();
boolean isOffloadedScanBatchingSupported = bluetoothAdapter.isOffloadedScanBatchingSupported();
Log.d(TAG, "Hardware support:"
+ " isMultipleAdvertisementSupported=" + isMultipleAdvertisementSupported
+ " isOffloadedFilteringSupported=" + isOffloadedFilteringSupported
+ " isOffloadedScanBatchingSupported=" + isOffloadedScanBatchingSupported);
try {
mGattServer = bluetoothManager.openGattServer(getApplicationContext(), new BluetoothGattServerCallback() {
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId,
int offset, BluetoothGattCharacteristic characteristic) {
if (mGattServer != null) {
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, new byte[]{ response++ });
Log.d(TAG, "Sent response");
}
}
});
UUID serviceUUID = UUID.fromString("6855f2ce-8dc6-4228-8bec-531167e0266b");
UUID characteristicUUID = UUID.fromString("09de1235-6594-4a2b-8d88-ad5eb8cc57c6");
UUID descriptorUUID = UUID.fromString("c3a29c57-7a4b-492c-b7c4-7d807f9639d2");
Log.d(TAG, "Service = " + serviceUUID);
Log.d(TAG, "characteristic = " + characteristicUUID);
Log.d(TAG, "descriptor = " + descriptorUUID);
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(
characteristicUUID, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);
characteristic.setValue(77, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(descriptorUUID,
BluetoothGattDescriptor.PERMISSION_READ);
characteristic.addDescriptor(descriptor);
BluetoothGattService service = new BluetoothGattService(serviceUUID, BluetoothGattService.SERVICE_TYPE_PRIMARY);
service.addCharacteristic(characteristic);
mGattServer.addService(service);
// advertise
BluetoothLeAdvertiser advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
if (advertiser != null) {
startAdvertising(serviceUUID, advertiser);
}
} catch (Throwable t) {
Log.e(TAG, FAILED_TO_CREATE_GATT_SERVER, t);
Toast.makeText(this, FAILED_TO_CREATE_GATT_SERVER, Toast.LENGTH_LONG).show();
return;
}
}
private void startAdvertising(UUID serviceUUID, BluetoothLeAdvertiser advertiser) {
Log.d(TAG, "startAdvertising()");
Toast.makeText(this, START_ADVERTISING, Toast.LENGTH_SHORT).show();
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setConnectable(true)
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.build();
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.addServiceUuid(new ParcelUuid(serviceUUID))
.setIncludeTxPowerLevel(true)
.build();
advertiser.startAdvertising(settings, data, new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mGattServer != null)
mGattServer.close();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Toast.makeText(this, "API = " + currentapiVersion, Toast.LENGTH_SHORT).show();
if (currentapiVersion >= 21)
startPeripheralGattServer();
else
Log.d(TAG, "BLE not supported");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
日志:
05-21 11:13:33.741 26925-26925/? E/Zygote﹕ MountEmulatedStorage()
05-21 11:13:33.741 26925-26925/? E/Zygote﹕ v2
05-21 11:13:33.741 26925-26925/? I/libpersona﹕ KNOX_SDCARD checking this for 10012
05-21 11:13:33.741 26925-26925/? I/libpersona﹕ KNOX_SDCARD not a persona
05-21 11:13:33.751 26925-26925/? I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-N9005_4.4.2 ver=40
05-21 11:13:33.751 26925-26925/? I/SELinux﹕ Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-N9005_5.0_0009
05-21 11:13:33.751 26925-26925/? E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
05-21 11:13:33.751 26925-26925/? I/art﹕ Late-enabling -Xcheck:jni
05-21 11:13:33.791 26925-26925/? D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
05-21 11:13:33.791 26925-26925/? D/ActivityThread﹕ Added TimaKeyStore provider
05-21 11:13:33.831 26925-26925/project.name.myapplication D/ResourcesManager﹕ creating new AssetManager and set to /data/app/project.name.myapplication-1/base.apk
05-21 11:13:33.981 26925-26925/project.name.myapplication D/BleTest﹕ startPeripheralGattServer()
05-21 11:13:33.991 26925-26925/project.name.myapplication D/BleTest﹕ Hardware support: isMultipleAdvertisementSupported=false isOffloadedFilteringSupported=true isOffloadedScanBatchingSupported=false
05-21 11:13:33.991 26925-26925/project.name.myapplication D/BluetoothGattServer﹕ registerCallback()
05-21 11:13:34.001 26925-26925/project.name.myapplication D/BluetoothGattServer﹕ registerCallback() - UUID=df2bf3e0-f7b7-4be1-9c73-338eec1eeda7
05-21 11:13:34.001 26925-26934/project.name.myapplication D/BluetoothGattServer﹕ onServerRegistered() - status=0 serverIf=6
05-21 11:13:34.001 26925-26925/project.name.myapplication D/BleTest﹕ Service = 6855f2ce-8dc6-4228-8bec-531167e0266b
05-21 11:13:34.001 26925-26925/project.name.myapplication D/BleTest﹕ characteristic = 09de1235-6594-4a2b-8d88-ad5eb8cc57c6
05-21 11:13:34.001 26925-26925/project.name.myapplication D/BleTest﹕ descriptor = c3a29c57-7a4b-492c-b7c4-7d807f9639d2
05-21 11:13:34.001 26925-26925/project.name.myapplication D/BluetoothGattServer﹕ addService() - service: 6855f2ce-8dc6-4228-8bec-531167e0266b
05-21 11:13:34.011 26925-26936/project.name.myapplication D/BluetoothGattServer﹕ onServiceAdded() - service=6855f2ce-8dc6-4228-8bec-531167e0266bstatus=0
05-21 11:13:34.011 26925-26925/project.name.myapplication E/BluetoothAdapter﹕ bluetooth le advertising not supported
05-21 11:13:34.011 26925-26925/project.name.myapplication D/Activity﹕ performCreate Call secproduct feature valuefalse
05-21 11:13:34.011 26925-26925/project.name.myapplication D/Activity﹕ performCreate Call debug elastic valuetrue
05-21 11:13:34.031 26925-26971/project.name.myapplication D/OpenGLRenderer﹕ Render dirty regions requested: true
05-21 11:13:34.071 26925-26971/project.name.myapplication I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.025_msm8974_LA.BF.1.1_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.25.01.03
Build Date: 11/19/14 Wed
Local Branch: mybranch5813579
Remote Branch: quic/LA.BF.1.1_rb1.11
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.025 + 30e7589 + NOTHING
05-21 11:13:34.071 26925-26971/project.name.myapplication I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-21 11:13:34.101 26925-26971/project.name.myapplication I/OpenGLRenderer﹕ HWUI protection enabled for context , &this =0xafa22088 ,&mEglDisplay = 1 , &mEglConfig = 8
05-21 11:13:34.101 26925-26971/project.name.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
05-21 11:13:34.241 26925-26925/project.name.myapplication I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@248edbf4 time:3431520
05-21 11:13:38.581 26925-26925/project.name.myapplication I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@248edbf4 time:3435866
05-21 11:13:49.811 26925-26932/project.name.myapplication I/art﹕ Ignoring second debugger -- accepting and dropping
PS。实际上,我不确定我的设备是否被发现 - 它的标题是&#34;未知&#34;。
答案 0 :(得分:2)
根据此链接https://newcircle.com/s/post/1697/android_lollipop_bluetooth_andevcon,Android 5 BLE API的新功能在硬件抽象层或HAL中 - 这意味着它取决于蓝牙芯片组功能以及制造商是否在其更新时在其设备中启用它因此,必须首先运行这3个函数才能确定硬件是否支持这些功能。
bluetoothAdapter.isMultipleAdvertisementSupported();
bluetoothAdapter.isOffloadedFilteringSupported();
bluetoothAdapter.isOffloadedScanBatchingSupported();
答案 1 :(得分:1)
您的日志显示:&#34;不支持蓝牙广告&#34;。
虽然你是正确的,具有蓝牙V4.0和API 21的设备应该支持外围模式(能够做广告),但三星Galaxy Note 3似乎不是这样。
查看报告此问题的Samsung forum。