[英]Using fetchUuidsWithSdp() to prevent friendly name caching
定义:
我的非Android设备(NAD)是一种蓝牙设备,其名称从60循环到0,并以无限方式重置。
目的:我要做的是让我的Android设备尽可能接近地检测到该倒数,并发出与NAD计数器尽可能接近的警报。
我这样做是通过将功能绑定到屏幕上的按钮,并密切注意通过我的BroadcastReceiver设置的吐司来使设备的本机BluetoothAdapter手动启动startDiscovery(),它会更新屏幕上的Textviews,从而使我能够监视设备上的内容正在实时接收
要求:在这种情况下,系统和资源效率不是问题。
问题:(请注意代码中的第1部分和第2部分)我不确定使用fetchUuidsWithSdp()会如何帮助我,因为它正在更新的TextView仍然为空,并且EXTRA_NAME填充了Textview,这超出了意图返回操作ACTION_NAME_CHANGED是缓存的初始发现名称(即,我的应用程序在初始发现后未读取名称)。
我的代码可以在下面找到
对不起,如果有任何新手错误,我正在努力:)
public class BTBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//pulling the action name from the broadcasted intent
String action = intent.getAction();
if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
sToaster("StartedD");//show toast that Discovery has started
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
sToaster("EndedD");//show toast signifying end of discovery
/*
if(notfound){
mBTAdapter.startDiscovery();
}*/
}
else if(BluetoothDevice.ACTION_FOUND.equals(action)){
//when a device is found
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//make sure it's indeed my NAD device by checking MAC address
if(device.getAddress().equals(MACAddress)){
if(notfound){
//show device name on screen
sToaster("FOUND DEvice");
notfound = false;
NAD = device;
NameShower.setText(device.getName());
}
else{
//do nothing if it's the second time the device is found
}
}
}
else if(BluetoothDevice.ACTION_NAME_CHANGED.equals(action)){
//name changed
BluetoothDevice foundDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//make sure it's indeed my NAD device
if(foundDevice.equals(NAD)){
sToaster("Name Change!"); //show on screen that the name change intent has been caught
//PART1
//to prevent caching of the old device name StackOverflow article
//advised using this function i don't totally understand yet
//NAD.fetchUuidsWithSdp();
//either commented or not commented the outcome is the same (no refresh of the name)
//PART2
//tried showing the new name two different ways below, neither of which are effective
//by inspecting the TextViews on-screen
NameShower.setText(foundDevice.getName());
EventView.setText(intent.getStringExtra(BluetoothDevice.EXTRA_NAME));
}
}
}
};
我曾在一个蓝牙项目中工作过,我发现发现过程应该是一个Intent,可以在后台进行注册。 要发现范围内的设备,您只需调用BTDevice.startDiscovery()即可搜索它们。 通常,如果连续启用,则startDiscovery()会耗尽电池电量。 如果您愿意,我可以编辑此帖子以分享我用来扫描设备的摘要。 希望这可以帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.