如何解决蓝牙模块 JDY-08 找不到安卓设备
我有 1 个 JDY-08 MASTER,它扫描查找 myDeviceName 并在找到该设备名称时触发一个函数。
String get_ble_scan_data(void){
String final_result = "";
String result = "";//reset and declare
String extract = set_ble("AT+SCAN1");//scan for device name,signal strength and mac address
String extract2 = set_ble("AT+GETDCD");//get number of device found
//Now we are going to check for which extraction has the data we interested in
if((extract.length() > threshold or extract.length() > threshold)){
result = extract;//pass extract as result
if(extract2.length() > extract.length()){//check which is bigger
result = extract2;//pass extract2 as result if its bigger in length than extract
}
}
if(result.length() > 0){//add filter and execution here
String filter = result;//copy
result = "";// reset
while(filter.indexOf('=') > -1){// we use the char = as a seperator
filter = filter.substring(filter.indexOf('=') + 1);//remove strings before seperator
result += filter.substring(0,filter.indexOf('\n')) + ' '; //extract till newline character
filter = filter.substring(filter.indexOf(result) + result.length());//remove extracted result so we go on to next extraction of same result if there is more device picked up
detect
}
result.trim();//remove spaces at the end or start if any
final_result = result;
}
return final_result;
}
void ble_response(String search_result){
String scan_result = search_result;//do bluetooth scan
if(scan_result.indexOf(myDeviceName) > -1 ){//when data present in scan and it contains desired key
if(scan_result.indexOf(' ') == -1){//if only one ble is picked up
ble_macaddress = scan_result.substring(0,scan_result.indexOf(','));
scan_result = scan_result.substring(scan_result.indexOf(ble_macaddress) + 1 + ble_macaddress.length(),scan_result.length());//remove mac address
ble_strength = scan_result.substring(0,'));
ble_name = scan_result.substring(scan_result.indexOf(ble_strength) + 1 + ble_strength.length(),scan_result.length());//remove mac address
if(((int) ble_strength.toFloat()) >= trigger_threshold and ble_name == key){
trigger_action();
}
}else{//if multiple ble is picked up
String cut = "";
while(scan_result.indexOf(',') > -1){//while there is still result to be processed
cut = scan_result.substring(0,scan_result.indexOf(' '));
scan_result = scan_result.substring(scan_result.indexOf(cut) + 1 + cut.length(),scan_result.length());
if(cut.indexOf(myDeviceName) > -1){//only analyze if it contains key
ble_macaddress = cut.substring(0,cut.indexOf(','));
cut = cut.substring(cut.indexOf(ble_macaddress) + 1 + ble_macaddress.length(),cut.length());//remove mac address
ble_strength = cut.substring(0,'));
ble_name = cut.substring(cut.indexOf(ble_strength) + 1 + ble_strength.length(),cut.length());//remove mac address
if(((int) ble_strength.toFloat()) >= trigger_threshold and ble_name == key){
trigger_action();
break;
}
}
}
}
}
如果我通过一个按钮使用另一个 JDY-08 设备,它会找到该设备并触发操作:
if(!digitalRead(11)){//if button is pushed///////
delay(500);
set_ble("AT+NAMEmyDeviceName");//change ble name
while(!digitalRead(11));//do nothing while button is still pressed
delay(2000);//allow time before name change back
set_ble("AT+NAMEJDY-08");//change name back
但是当我使用手机时它不会触发动作:
private void advertise(){
final BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
final AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY )
.setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH )
.setConnectable( true )
.build();
final AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName( true )
.build();
final AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}
@Override
public void onStartFailure(int errorCode) {
Log.e( "BLE","Advertising onStartFailure: " + errorCode );
super.onStartFailure(errorCode);
}
};
advertiser.startAdvertising(settings,data,advertisingCallback);
final Handler myTimerHandler = new Handler();
myTimerHandler.postDelayed(
new Runnable()
{
@Override
public void run(){
advertiser.stopAdvertising(advertisingCallback);
}
},30000);
}
我也将 Intent 与 BluetoothAdapter.ACTION_REQUEST_disCOVERABLE 一起使用。
使用 NRFConnect 应用程序,我可以看到 JDY-08 按钮设备如何更改设备名称(这会触发 JDY-08 MASTER 上的操作)。我还可以看到带有 myDeviceName 的 Android 设备,但这不会触发操作。我是否遗漏了 Android 应用中的某些内容?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。