android sdk 5.0支持ble周边吗
答案:2 悬赏:0
解决时间 2021-02-21 01:41
- 提问者网友:傲气稳全场
- 2021-02-20 05:45
android sdk 5.0支持ble周边吗
最佳答案
- 二级知识专家网友:厌今念往
- 2021-02-20 07:04
具有低功耗蓝牙模块的设备可以扮演2个角色,中心,周边。周边是数据提供者,中心是数据接收/处理者。IOS设备可以很好的扮演这2个角色,利用现成的API就能开发出具有周边和中心功能的应用,我大Android就有点悲催了,自Android 4.3的系统就规定了BLE的API,但是仅限于中心,至于周边一直没有API的支持。直到2014.6.26 Android Lollipop的面世,才带来了周边API的支持(BluetoothLeAdvertiser)。
利用空闲时间看了看文档写了个一段关于BLE 周边的代码,当我兴冲冲的运行在Nexus5上面时,瞬间就crash了空指针异常,看了下源代码
[java] view plaincopy
public BluetoothLeAdvertiser getBluetoothLeAdvertiser() {
if (getState() != STATE_ON) {
return null;
}
if (!isMultipleAdvertisementSupported()) {
return null;
}
synchronized(mLock) {
if (sBluetoothLeAdvertiser == null) {
sBluetoothLeAdvertiser = new BluetoothLeAdvertiser(mManagerService);
}
}
return sBluetoothLeAdvertiser;
}
后来在官网上看了下,现在支持周边的设备只有Nexus6和Nexus9,虽然我很想入手Nexus6,但又不想转运和淘宝,无奈等吧.....说真的在中国开发Android真蛋疼~
最后贴下小代码,欢迎交流指导
[java] view plaincopy
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
BluetoothLeAdvertiser advertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
AdvertiseData data = new AdvertiseData.Builder()
.addServiceUuid(ParcelUuid.fromString(ADVERTISER_SERVICE_UUID)).build();
AdvertiseSettings settings = new AdvertiseSettings.Builder().setConnectable(true).build();
advertiser.startAdvertising(settings , data, new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}
});
利用空闲时间看了看文档写了个一段关于BLE 周边的代码,当我兴冲冲的运行在Nexus5上面时,瞬间就crash了空指针异常,看了下源代码
[java] view plaincopy
public BluetoothLeAdvertiser getBluetoothLeAdvertiser() {
if (getState() != STATE_ON) {
return null;
}
if (!isMultipleAdvertisementSupported()) {
return null;
}
synchronized(mLock) {
if (sBluetoothLeAdvertiser == null) {
sBluetoothLeAdvertiser = new BluetoothLeAdvertiser(mManagerService);
}
}
return sBluetoothLeAdvertiser;
}
后来在官网上看了下,现在支持周边的设备只有Nexus6和Nexus9,虽然我很想入手Nexus6,但又不想转运和淘宝,无奈等吧.....说真的在中国开发Android真蛋疼~
最后贴下小代码,欢迎交流指导
[java] view plaincopy
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
BluetoothLeAdvertiser advertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
AdvertiseData data = new AdvertiseData.Builder()
.addServiceUuid(ParcelUuid.fromString(ADVERTISER_SERVICE_UUID)).build();
AdvertiseSettings settings = new AdvertiseSettings.Builder().setConnectable(true).build();
advertiser.startAdvertising(settings , data, new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}
});
全部回答
- 1楼网友:傲娇菇凉
- 2021-02-20 08:37
为使得你的app在android获得更好的表现,请将你的targetsdkversion设置成21。调用最新的android 5.0 api的时候要注意在调用前判断系统版本号以兼容之前的系统版本。不能使用低于minsdkversion的api。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯