[英]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.