【发布时间】:2021-05-25 12:37:37
【问题描述】:
我有 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, scan_result.indexOf(','));
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, cut.indexOf(','));
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);
}
我还将意图与 BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE 一起使用。
使用 NRFConnect 应用程序,我可以看到 JDY-08 Button 设备如何更改设备名称(这会触发 JDY-08 MASTER 上的操作)。我还可以看到带有 myDeviceName 的 Android 设备,但这不会触发操作。我是否错过了 Android 应用中的某些内容?
【问题讨论】:
-
您想在 JDY-08 Master 检测到您的 Android 手机后立即对其进行操作?
-
是的,只要它检测到带有 myDeviceName 的 android 手机。
-
您说我也可以看到带有 myDeviceName 的 Android 设备,但这不会触发操作。 您在哪里看到 myDeviceName?您是否尝试过将调试输出添加到
void ble_response(String search_result)以查看哪些设备被拾取以及信号强度如何 -
我可以在 NRFConnect 应用程序中看到它。我在 myDeviceName 中看到了两者,但只有 JDY-08 触发了该操作。我不知道如何在 JDY-08 MASTER 上调试,我的任务是让手机将其名称更改为 myDeviceName 为 SLAVE,因为 JDY-08 正在寻找带有 myDeviceName 的 SLAVES。
-
抱歉耽搁了,我正试图找出代码在哪里运行。
标签: java android bluetooth bluetooth-lowenergy