我正在为BLE广告制作一个应用程序,但只要我按下开始发送按钮,我就会收到吐司消息status
为18,当我检查logcat时,我看到错误代码1, 10 and 14
。
我还在manifest.xml
文件中包含了所有权限。
以下是广告代码:
public void onAdvertiseButtonPressed(View view){
final Button advertiseButton=(Button)view;
Log.i(TAG,"Advertise mode:"+advertiseMode);
Log.i(TAG,"Advertise power:"+advertisePower);
AdvertiseData advertiseData = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.build();
BluetoothLeAdvertiser bluetoothLeAdvertiser=BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
AdvertisingSetParameters parameters = new AdvertisingSetParameters.Builder()
.setInterval(advertiseInterval)
.setTxPowerLevel(advertisePower)
.setScannable(true)
.setConnectable(false)
.build();
AdvertisingSetCallback advertisingSetCallback = new AdvertisingSetCallback() {
@Override
public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
super.onAdvertisingSetStarted(advertisingSet, txPower, status);
Log.i(TAG,"Advertising set started:::" + advertisingSet + ",txPower:"+txPower+",status:"+status);
if(advertisingSet!=null) {
advertisingSet.enableAdvertising(true, 0, 0);
advertiseButton.setEnabled(false);
}
else{
Toast.makeText(TransmitActivity.this, "Start adv error status:"+status, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
super.onAdvertisingSetStopped(advertisingSet);
Log.i(TAG,"Advertising set stopped");
activeAdvertiseCallback = null;
handler.postDelayed(() -> {
advertiseButton.setEnabled(true);
if(!isStopped)
onAdvertiseButtonPressed(advertiseButton);
}, 1);
}
@Override
public void onAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, int status) {
super.onAdvertisingEnabled(advertisingSet, enable, status);
Log.i(TAG,"Advertising set enabled:"+enable + "status:" + status);
if(enable){
activeAdvertiseCallback = this;
handler.postDelayed(()-> {
bluetoothLeAdvertiser.stopAdvertisingSet(this);
},1);
}
}
};
if (bluetoothLeAdvertiser == null) {
Log.e(TAG, "Advertiser not found, isBluetoothOn " + BluetoothAdapter.getDefaultAdapter().isEnabled());
Toast.makeText(this, "Advertiser not found, isBluetoothOn " + BluetoothAdapter.getDefaultAdapter().isEnabled(), Toast.LENGTH_SHORT).show();
} else {
time = System.currentTimeMillis ();
bluetoothLeAdvertiser.startAdvertisingSet(parameters, advertiseData, null, null, null, advertisingSetCallback);
}
}
字符串
Logcat:
2020-06-28 23:16:09.471 11505-11505/com.example.bleexperiments E/CheckPermission: _bluetooth code = 1
2020-06-28 23:16:10.736 11505-11505/com.example.bleexperiments E/ColorViewRootUtil: nav gesture mode swipeFromBottom ignore false downY 951 mScreenHeight 2340 mScreenWidth 1080 mStatusBarHeight 54 globalScale 1.125 nav mode 3 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=114.0, y[0]=951.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=330086863, downTime=330086863, deviceId=6, source=0x1002, displayId=0 } rotation 0
2020-06-28 23:16:14.980 11505-11505/com.example.bleexperiments E/ColorViewRootUtil: nav gesture mode swipeFromBottom ignore false downY 1389 mScreenHeight 2340 mScreenWidth 1080 mStatusBarHeight 54 globalScale 1.125 nav mode 3 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=763.0, y[0]=1389.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=330091106, downTime=330091106, deviceId=6, source=0x1002, displayId=0 } rotation 0
2020-06-28 23:16:15.097 11505-11505/com.example.bleexperiments E/CheckPermission: _bluetooth code = 14
2020-06-28 23:16:15.099 11505-11505/com.example.bleexperiments E/CheckPermission: _bluetooth code = 14
2020-06-28 23:16:15.102 11505-11505/com.example.bleexperiments E/CheckPermission: _bluetooth code = 10
2020-06-28 23:18:06.785 11505-11505/com.example.bleexperiments E/ColorViewRootUtil: nav gesture mode swipeFromBottom ignore false downY 1414 mScreenHeight 2340 mScreenWidth 1080 mStatusBarHeight 54 globalScale 1.125 nav mode 3 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=837.0, y[0]=1414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=330202905, downTime=330202905, deviceId=6, source=0x1002, displayId=0 } rotation 0
2020-06-28 23:18:06.872 11505-11505/com.example.bleexperiments E/CheckPermission: _bluetooth code = 14
2020-06-28 23:18:06.874 11505-11505/com.example.bleexperiments E/CheckPermission: _bluetooth code = 14
2020-06-28 23:18:06.878 11505-11505/com.example.bleexperiments E/CheckPermission: _bluetooth code = 10
型
1条答案
按热度按时间pw136qt21#
我遇到了和你一样的问题,我的解决方案是,而不是使用startAdvertisingSet,使用startAdvertising。唯一的缺点是无法设置广播间隔。如果没有特别的要求,我觉得用startAdvertising代替是个不错的选择。