蓝牙技术(一)

第二种:不会有系统的提示界面

 1 //搜索蓝牙设备
 2 public class MainActivity extends ActionBarActivity {
 3     private BluetoothAdapter adapter;
 4     TextView textDevice;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
10         setContentView(R.layout.fragment_main);
11         textDevice = (TextView) findViewById(R.id.tv);
12         adapter = BluetoothAdapter.getDefaultAdapter();
13         Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
14         if (pairedDevices.size() > 0) {
15             for (BluetoothDevice device : pairedDevices) {
16 
17                 textDevice.append(device.getName() + ":" + device.getAddress());
18 
19             }
20         }
21         // 设备被找到--发送广播
22         IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
23         this.registerReceiver(receiver, filter);
24         // 全部搜索完发送--广播
25         filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
26         this.registerReceiver(receiver, filter);
27     }
28 
29     public void click(View view) {
30         setProgressBarIndeterminateVisibility(true);
31         setTitle("正在扫描。。。");
32         if (adapter.isDiscovering()) {
33             adapter.cancelDiscovery();
34         }
35         adapter.startDiscovery();
36 
37     }
38 
39     private final BroadcastReceiver receiver = new BroadcastReceiver() {
40 
41         @Override
42         public void onReceive(Context context, Intent intent) {
43             // TODO Auto-generated method stub
44             String action = intent.getAction();
45             if (BluetoothDevice.ACTION_FOUND.equals(action)) {
46                 BluetoothDevice device = intent
47                         .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
48                 if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
49                     textDevice.append(device.getName() + ":"
50                             + device.getAddress() + "\n");
51 
52                 }
53 
54             } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
55                     .equals(action)) {
56                 setProgressBarVisibility(false);
57                 setTitle("完成");
58             }
59         }
60 
61     };
62 }

 

转载于:https://www.cnblogs.com/my334420/p/6915979.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值