为什么BluetoothAdapter.startDiscovery(..)需要获得蓝牙设备播放? [英] Why BluetoothAdapter.startDiscovery(..) require to get Bluetooth device broadcast?
本文介绍了为什么BluetoothAdapter.startDiscovery(..)需要获得蓝牙设备播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常情况下如何Android的广播工作是:应用程序必须创建广播接收器,并有注册意向的行动为它想获得接收事件
。但在蓝牙设备发现/扫描的情况下,为什么它需要通过请求调用的 BluetoothAdapter.startDsiccovery()
基本上,我想通过万岁服务在后台运行。
任何一个有想法吗?
解决方案
私人无效listenPairedDevice(){
按钮listenBtn =(按钮)findViewById(R.id.button_listen);
listenBtn.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看视图){
意图盘=新意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(光盘,DISCOVERY_REQUEST);
}
});
}
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
如果(要求code == DISCOVERY_REQUEST){
布尔isDiscoverable =结果code> 0;
如果(isDiscoverable){
字符串名称=bluetoothserver;
尝试{
最后BluetoothServerSocket btserver = bluetooth.listenUsingRfcommWithServiceRecord(名称,UUID);
AsyncTask的<整型,无效的BluetoothSocket> acceptThread =新的AsyncTask<整型,无效的BluetoothSocket>(){ @覆盖
受保护的BluetoothSocket doInBackground(整数... PARAMS){
尝试{ 插座= btserver.accept();
返回插座;
}赶上(IOException异常五){
Log.d(蓝牙,e.getMessage());
}
最后{
//关闭声明中后来又增加由MR
尝试{
btserver.close();
}赶上(IOException异常五){ }
}
返回null;
} @覆盖
保护无效onPostExecute(的BluetoothSocket结果){
如果(结果!= NULL)
changeLayout();
}
};
acceptThread.execute(结果code);
}赶上(IOException异常五){
Log.d(蓝牙,e.getMessage());
}
}
}
Normally how Android Broadcast work is: app have to create BroadcastReceiver and have to register action intent for it want to get receive event.
But in case of Bluetooth device discovery/scanning why it required request call through BluetoothAdapter.startDsiccovery().
Basically I want to dicover BLE device through long live Service running in Background.
Any one have idea here?
解决方案
private void listenPairedDevice() {
Button listenBtn = (Button)findViewById(R.id.button_listen);
listenBtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent disc = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(disc, DISCOVERY_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DISCOVERY_REQUEST) {
boolean isDiscoverable = resultCode > 0;
if (isDiscoverable) {
String name = "bluetoothserver";
try {
final BluetoothServerSocket btserver = bluetooth.listenUsingRfcommWithServiceRecord(name, uuid);
AsyncTask<Integer, Void, BluetoothSocket> acceptThread = new AsyncTask<Integer, Void, BluetoothSocket>() {
@Override
protected BluetoothSocket doInBackground(Integer... params) {
try {
socket = btserver.accept();
return socket;
} catch (IOException e) {
Log.d("BLUETOOTH", e.getMessage());
}
finally {
//close statement added later by MR
try{
btserver.close();
} catch (IOException e){
}
}
return null;
}
@Override
protected void onPostExecute(BluetoothSocket result) {
if (result != null)
changeLayout();
}
};
acceptThread.execute(resultCode);
} catch (IOException e) {
Log.d("BLUETOOTH", e.getMessage());
}
}
}
这篇关于为什么BluetoothAdapter.startDiscovery(..)需要获得蓝牙设备播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文