zoukankan      html  css  js  c++  java
  • 蓝牙技术(一)

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

     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() + "
    ");
    51 
    52                 }
    53 
    54             } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
    55                     .equals(action)) {
    56                 setProgressBarVisibility(false);
    57                 setTitle("完成");
    58             }
    59         }
    60 
    61     };
    62 }
  • 相关阅读:
    55个高质量个性的Photoshop的笔刷
    60个Web设计师必看的教程
    30个高质量的旅游网站设计
    65非常棒的photoshop框架笔刷
    55个非常有创意的博客和出版字体
    30个最好的免费的WordPress主题
    15非常酷且反应快的jQuery slider插件
    65个漂亮的WordPress博客主题
    45个触发创作灵感的技术类网站设计资源
    55个高质量的Magento主题,助你构建电子商务站点
  • 原文地址:https://www.cnblogs.com/my334420/p/6915979.html
Copyright © 2011-2022 走看看