蓝牙模块 JDY-08 未找到 Android 设备
Bluetooth module JDY-08 not finding Android device
我有 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
但是当我使用 phone 它不会触发操作:
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-Master只接收到JDY设备的名称。所有其他设备仅显示 mac 地址和信号强度。
我有 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
但是当我使用 phone 它不会触发操作:
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-Master只接收到JDY设备的名称。所有其他设备仅显示 mac 地址和信号强度。