zoukankan      html  css  js  c++  java
  • Android蓝牙操作

    1.添加蓝牙权限

     <uses-permission android:name = "android.permission.BLUETOOTH"/>
        <!--启用应用启动设备发现或者操作蓝牙设备的超级管理员-->
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    2.获取蓝牙设备

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
                        Toast.makeText(MainActivity.this, "未找到设备", Toast.LENGTH_LONG).show();
                        return;
                    }
    
    if (!bluetoothAdapter.isEnabled()) {
                        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
                    }

    3.搜索设备

    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
            if (pairedDevices.size() > 0) {
                // Loop through paired devices
                for (final BluetoothDevice device : pairedDevices) {
                    // Add the name and address to an array adapter to show in a ListView
                    final Button btnDevice = findViewById(R.id.device_btn);
                    btnDevice.setText(device.getAddress());
                    btnDevice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {try {
                                new ConnectThread(device).run();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }

    4.连接设备

    private class ConnectThread extends Thread {
            private BluetoothDevice mDevice;
            private BluetoothSocket mSocket;
    
            public ConnectThread(BluetoothDevice device) throws IOException {
                mDevice = device;
                mSocket = device.createInsecureRfcommSocketToServiceRecord(UUID.randomUUID());
                Log.d("aaa", "start");
            }
    
            public void run() {
                // 关闭发现设备
                bluetoothAdapter.cancelDiscovery();
                try {
                    mSocket.connect();
                } catch (IOException connectException) {
                    try {
                        mSocket.close();
                    } catch (IOException closeException) {
                        return;
                    }
                }
                Log.d("aaa", "connected");
                // 自定义方法
                manageConnectedSocket(mSocket);
            }
    
            private void manageConnectedSocket(BluetoothSocket socket) {
                //读取数据
            }
    
            public void cancle() {
                try {
                    mSocket.close();
                } catch (IOException closeException) {
    
                }
            }
        }
  • 相关阅读:
    关于程序员认知和编程学习,没有任何一篇文章会讲得如此透彻
    Found 1 slaves: Use of uninitialized value in printf at /usr/local/percona-toolkit/bin/pt-online-schema-change line 8489
    alert 多语言的处理
    #!/bin/sh & #!/bin/bash区别
    mysql 常用
    java.io.FileNotFoundException
    struts1 & jquery form 文件异步上传
    简单的数据库连接池实例(java语言)
    null id in com.rocky.** entry 错误处理
    java unsupported major.minor version 51.0 解决
  • 原文地址:https://www.cnblogs.com/punkrocker/p/11967111.html
Copyright © 2011-2022 走看看