Android的蓝牙BluetoothDevice.ACTION_FOUND没有被触发?

前端之家收集整理的这篇文章主要介绍了Android的蓝牙BluetoothDevice.ACTION_FOUND没有被触发?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从我的应用程序管理多个蓝牙事件,因此用户无需离开应用程序&尝试从 Android设置搜索/配对蓝牙设备.

我能够枚举以前配对的设备,并开始发现,但我无法找到附近的设备.

背景资料:

设备=三星Galaxy S6

OS = Android 6.0.1,内核3.4.0-750027

通过Android内置的发现可以看到蓝牙设备

这是我的相关代码

  1. package experiment.xyz.abc;
  2.  
  3. import android.app.ListActivity;
  4. import android.bluetooth.BluetoothAdapter;
  5. import android.bluetooth.BluetoothDevice;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.os.Bundle;
  11. import android.os.Parcelable;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.AdapterView;
  15. import android.widget.Button;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. import java.lang.reflect.Method;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Set;
  23.  
  24. /**
  25. *
  26. * This activity is responsible for bluetooth device discovery,pairing,and selection.
  27. * 1) user can search for nearby devices
  28. * 2) pair with device
  29. * 3) unpair a device
  30. * 4) select a bt device( upon successful selection the activity navigates away).
  31. */
  32. public class BluetDeviceListActivity extends ListActivity
  33. {
  34. public static String BLUETOOTH_DEVICE_ADDRESS= "BLUETOOTH_DEVICE_ADDRESS";
  35.  
  36. List<BluetoothDevice> bluetList;
  37. BluetoothDeviceListAdapter newBluetoothDeviceListAdapter = null;
  38. private Set<BluetoothDevice> pairedDevices;
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42.  
  43. setContentView(R.layout.bluet_list_view);
  44.  
  45. //instantiate adapter
  46. newBluetoothDeviceListAdapter = new BluetoothDeviceListAdapter(this);
  47.  
  48.  
  49. //populate bluet list
  50. populatePairedBluetoothDevices();
  51.  
  52. //set the ListActivity's adapter with our custom adapter
  53. setListAdapter(newBluetoothDeviceListAdapter);
  54. //on item click get the associated item based index & use Intent to send BluetoothDevice
  55. getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
  56. @Override
  57. public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
  58. BluetoothDevice bluetoothDevice= newBluetoothDeviceListAdapter.getListOfBluetoothDevices().get(position);
  59.  
  60. if (bluetoothDevice!=null) {
  61. Intent returnIntent = new Intent();
  62. returnIntent.putExtra(BLUETOOTH_DEVICE_ADDRESS,bluetoothDevice.getAddress());
  63. setResult(RESULT_OK,returnIntent);
  64. }
  65.  
  66. finish();
  67.  
  68. if (mReceiver!=null)
  69. {
  70.  
  71. try {
  72. unregisterReceiver(mReceiver);
  73. }
  74. catch (IllegalArgumentException exc)
  75. {
  76. exc.printStackTrace();
  77. }
  78. }
  79. }
  80. });
  81.  
  82. //cancel activity dialog
  83. Button cancelButton = (Button) findViewById(R.id.cancelBluetoothDialogButton);
  84. cancelButton.setOnClickListener( new View.OnClickListener() {
  85.  
  86. @Override
  87. public void onClick(View v) {
  88.  
  89. finish();
  90.  
  91. if (mReceiver!=null)
  92. {
  93.  
  94. try {
  95. unregisterReceiver(mReceiver);
  96. }
  97. catch (IllegalArgumentException exc)
  98. {
  99. exc.printStackTrace();
  100. }
  101. }
  102. }
  103. });
  104.  
  105. //search for bluetooth devices
  106. Button searchAndPairButton = (Button) findViewById(R.id.searchPairButton);
  107. searchAndPairButton.setOnClickListener( new View.OnClickListener() {
  108.  
  109. @Override
  110. public void onClick(View v) {
  111.  
  112.  
  113. //update textview
  114. populateDiscoveredBluetoothDevices();
  115. }
  116. });
  117.  
  118.  
  119. //pair or unpair selected device
  120. Button pairOrUnpairButton = (Button) findViewById(R.id.pairUnpairDialogButton);
  121. pairOrUnpairButton.setOnClickListener( new View.OnClickListener() {
  122.  
  123. @Override
  124. public void onClick(View v) {
  125.  
  126. //TODO:
  127.  
  128. }
  129. });
  130.  
  131.  
  132.  
  133. }//end onCreate
  134.  
  135.  
  136. //discover nearby devices & add to list
  137. private void populateDiscoveredBluetoothDevices()
  138. {
  139. StreamApiUtility.getBluetoothAdapter();
  140.  
  141.  
  142. //is bluet enabled
  143. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  144. if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
  145.  
  146. //if already discovering cancel
  147. if(mBluetoothAdapter.isDiscovering()){
  148. mBluetoothAdapter.cancelDiscovery();
  149. }
  150.  
  151. //start a new discovery
  152. mBluetoothAdapter.startDiscovery();
  153.  
  154.  
  155. }//end there are paired devices
  156. else
  157. {
  158. Log.i("EEGdataCapture","No paired devices,select Search & Pair.");
  159. TextView textView =(TextView) findViewById(R.id.textViewBluetoothListInstruction);
  160. textView.setText("No paired devices,select Search & Pair.");
  161.  
  162. }
  163.  
  164. }
  165.  
  166.  
  167. //query already paired & add to list
  168. private void populatePairedBluetoothDevices()
  169. {
  170. StreamApiUtility.getBluetoothAdapter();
  171.  
  172. //is bluet enabled
  173. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  174. if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
  175.  
  176. //if already discovering cancel
  177. if(mBluetoothAdapter.isDiscovering()){
  178. mBluetoothAdapter.cancelDiscovery();
  179. }
  180.  
  181. //start a new discovery
  182. mBluetoothAdapter.startDiscovery();
  183.  
  184. //iterate paired/bonded devices and if there are any add them to the custom adapter
  185. pairedDevices = mBluetoothAdapter.getBondedDevices();
  186. if (pairedDevices.size() > 0) {
  187. BluetoothDevice bluetoothDevice;
  188. Iterator<BluetoothDevice> it = pairedDevices.iterator();
  189. while (it.hasNext()) {
  190. bluetoothDevice = (BluetoothDevice) it.next();
  191. //add to adapter
  192. newBluetoothDeviceListAdapter.addDevice(bluetoothDevice);
  193. newBluetoothDeviceListAdapter.notifyDataSetChanged();
  194. Log.i("EEGdataCapture","paired device,name: " + bluetoothDevice.getName() + ",address: " + bluetoothDevice.getAddress());
  195. }
  196.  
  197.  
  198. }//end there are paired devices
  199. else
  200. {
  201. Log.i("EEGdataCapture",select Search & Pair.");
  202. TextView textView =(TextView) findViewById(R.id.textViewBluetoothListInstruction);
  203. textView.setText("No paired devices,select Search & Pair.");
  204.  
  205. }
  206.  
  207. }
  208. }
  209.  
  210.  
  211.  
  212. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  213. public void onReceive(Context context,Intent intent) {
  214. String action = intent.getAction();
  215. BluetoothDevice device;
  216.  
  217. if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  218.  
  219. device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  220.  
  221.  
  222. Log.i("EEGdataCapture","BluetoothDevice.ACTION_FOUND");
  223.  
  224. Log.i("EEGdataCapture",device.getName() + "\n" + device.getAddress());
  225.  
  226. //update list
  227.  
  228. newBluetoothDeviceListAdapter.addDevice(device);
  229. newBluetoothDeviceListAdapter.notifyDataSetChanged();
  230.  
  231.  
  232. }
  233. else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
  234.  
  235. Log.i("EEGdataCapture","BluetoothDevice.ACTION_BOND_STATE_CHANGED");
  236.  
  237. final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.ERROR);
  238. final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIoUS_BOND_STATE,BluetoothDevice.ERROR);
  239.  
  240. if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
  241.  
  242. Toast.makeText(MyApp.getAppContext(),"Paired",Toast.LENGTH_SHORT).show();
  243. } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED) {
  244. Toast.makeText(MyApp.getAppContext(),"Unpaired",Toast.LENGTH_SHORT).show();
  245. }
  246.  
  247.  
  248. }
  249. else if (BluetoothDevice.ACTION_UUID.equals(action)) {
  250.  
  251. Log.i("EEGdataCapture","BluetoothDevice.ACTION_UUID");
  252.  
  253.  
  254.  
  255. }
  256.  
  257. else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
  258.  
  259. Log.i("EEGdataCapture","BluetoothAdapter.ACTION_DISCOVERY_STARTED,Discovery Started...");
  260.  
  261.  
  262. }
  263.  
  264. else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
  265. Log.i("EEGdataCapture","BluetoothAdapter.ACTION_DISCOVERY_FINISHED,Discovery finished.");
  266.  
  267.  
  268. }
  269. else
  270. {
  271. Log.i("EEGdataCapture","BluetoothAdapter,ACTIOM is not supported,action ="+action);
  272. }
  273.  
  274.  
  275. }};
  276.  
  277.  
  278.  
  279. private void pairDevice(BluetoothDevice device) {
  280. try {
  281. Method method = device.getClass().getMethod("createBond",(Class[]) null);
  282. method.invoke(device,(Object[]) null);
  283. } catch (Exception e) {
  284. e.printStackTrace();
  285. }
  286. }
  287.  
  288. private void unpairDevice(BluetoothDevice device) {
  289. try {
  290. Method method = device.getClass().getMethod("removeBond",(Object[]) null);
  291.  
  292. } catch (Exception e) {
  293. e.printStackTrace();
  294. }
  295. }
  296.  
  297.  
  298.  
  299. @Override
  300. protected void onPause() {
  301. super.onPause();
  302.  
  303. if ( StreamApiUtility.getBluetoothAdapter()!=null) {
  304. StreamApiUtility.getBluetoothAdapter().cancelDiscovery();
  305. }
  306. if (mReceiver!=null)
  307. {
  308.  
  309. try {
  310. unregisterReceiver(mReceiver);
  311. }
  312. catch (IllegalArgumentException exc)
  313. {
  314. exc.printStackTrace();
  315. }
  316. }
  317. }
  318.  
  319. @Override
  320. protected void onResume()
  321. {
  322. super.onResume();
  323. //filter to capture bluet events
  324. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  325. filter.addAction(BluetoothDevice.ACTION_UUID);
  326. filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
  327. filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
  328.  
  329. //register listener for bluet events before starting discovery again
  330. registerReceiver(mReceiver,filter);
  331.  
  332. }
  333.  
  334.  
  335. @Override
  336. protected void onDestroy() {
  337. super.onDestroy();
  338.  
  339. if (mReceiver!=null)
  340. {
  341.  
  342. try {
  343. unregisterReceiver(mReceiver);
  344. }
  345. catch (IllegalArgumentException exc)
  346. {
  347. exc.printStackTrace();
  348. }
  349. }
  350. }
  351. }

更新的清单:

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  2. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  3. <uses-permission android:name="android.permission.BLUETOOTH" />
  4. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  5. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

尽管正在调用ACTION_DISCOVERY_STARTED或ACTION_DISCOVERY_FINISHED,但不会触发/调用BroadcastReciever的BluetoothDevice.ACTION_FOUND.

请帮忙???

谢谢

解决方法

你的代码似乎没问题.我猜你有BLUETOOTH权限(否则你不会收到ACTION_DISCOVERY_STARTED),但你是否也拥有ACCESS_COARSE_LOCATION权限?

You need it to receive ACTION_FOUND

ACTION_FOUND
Broadcast Action: Remote device discovered.
Requires BLUETOOTH and ACCESS_COARSE_LOCATION to receive.

猜你在找的Android相关文章