Android開發-藍牙-步驟方法

一、權限聲明

    在AndroidManifest.xml文件中manifest下添加以下代碼:

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    BLUETOOTH權限用於執行藍牙通信(例如請求連接、接受連接、傳輸數據)。

    BLUETOOTH_ADMIN權限用於初始化設備查找或控制藍牙設置。

    注意:如果你使用BLUETOOTH_ADMIN權限,那麼必須擁有BLUETOOTH權限。


二、Activity的成員變量

 在整個類中都可以被訪問的變量,以下省略部分UI組件變量。

    private BluetoothAdapter ba;
    
    private ArrayList<String> lname = new ArrayList<>();	// 用於儲存已匹配的藍牙設備信息
    private ArrayList<String> laddress = new ArrayList<>();
    private String sdname;
    private String sdaddress;

    private BluetoothSocket bsocket = null;
    private OutputStream ostream = null;
    private InputStream istream = null;

    private static final UUID SerialPort_UUID = UUID.fromString(
            "00001101-0000-1000-8000-00805F9B34FB");

    private Handler hd;
    private ReceiveThread rt;<span style="white-space:pre">	</span>// 自定義的用於接收藍牙消息的內部類

    BluetoothAdapter代表本地藍牙適配器(藍牙無線電),是所有藍牙交互的入口。

    BluetoothSocket代表一個藍牙socket的接口(和TCP Socket類似)。這是一個連接點,它允許一個應用與其他藍牙設備通過InputStream和OutputStream交換數據。

    UUID,通用唯一識別碼。藍牙串口服務的UUID碼:SerialPortServiceClass_UUID = ‘{00001101-0000-1000-8000-00805F9B34FB}’。


三、獲取藍牙適配器並開啓藍牙功能

    這個步驟一般在Activity的OnCreate()方法中實現,以下省略部分UI代碼,幷包含用於處理藍牙接收線程傳來的消息的Handler。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ba = BluetoothAdapter.getDefaultAdapter();<span style="white-space:pre">	</span>// 獲取藍牙適配器

        hd = new Handler() {<span style="white-space:pre">				</span>// 用於處理藍牙接收線程傳來的消息的Handler
            public void handleMessage(Message msg)
            {
                super.handleMessage(msg);
                if(msg.what == 112) {
                    emessage.setText(Arrays.toString((byte[])msg.obj));
                    emessage.setEnabled(true);
                }
            }
        };

        // 檢測到Android設備不支持藍牙功能
        if (ba == null) {
            Toast.makeText(getApplicationContext(),
                    getResources().getString(R.string.dev_nsup_bt),
                    Toast.LENGTH_SHORT).show();
        }

        // 當藍牙功能未開啓,詢問開啓
        if (!ba.isEnabled()) {
            Intent turnOn = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnOn, 0);
        }

    }



四、搜索藍牙設備

    點擊按鈕,打開系統藍牙設置界面,自行配對。按鈕方法如下:

    public void SearchOnClick(View Source) {
        startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
    }


五、獲取已匹配設備的信息及連接藍牙設備

    // 藍牙設備選擇按鈕的點擊事件
    public void ChooseDevOnClick(View Source) {
        Set<BluetoothDevice> sdevices;
        // 清空設備數據
        lname.clear();
        laddress.clear();

        // 獲取已匹配的藍牙設備信息
        sdevices = ba.getBondedDevices();
        if (sdevices.size() > 0) {
            for (BluetoothDevice btd : sdevices) {
                lname.add(btd.getName());
                laddress.add(btd.getAddress());
                System.out.println(btd.getName() + btd.getAddress());
            }
        }

        // 將設備信息放入String數字
        int size = lname.size();
        final String[] snames = new String[size];
        final String[] saddresses = new String[size];
        for (int i = 0; i < size; i++) {
            snames[i] = lname.get(i);
            saddresses[i] = laddress.get(i);
        }
        // 創建顯示藍牙設備列表的AlertDialog
        AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this);
        ab.setTitle(getResources().getString(R.string.choose_bt_dev));
        ab.setItems(snames, new DialogInterface.OnClickListener() {<span style="white-space:pre">	</span>// 設置列表項的點擊事件
            public void onClick(DialogInterface dialog, int which) {
                // 獲取點擊的設備信息
                sdname = snames[which];
                sdaddress = saddresses[which];
                // 與選擇的設備進行藍牙連接
                BluetoothDevice device = ba.getRemoteDevice(sdaddress);
                try {
                    bsocket = device.createRfcommSocketToServiceRecord(
                            SerialPort_UUID);
                    ba.cancelDiscovery();
                    bsocket.connect();

                    // 開啓藍牙接收線程
                    rt = new ReceiveThread(bsocket);<span style="white-space:pre">	</span>// 定義見步驟五
                    rt.start();
                } catch (IOException e) {
                    // TODO: 顯示失敗提示
                    try {
                        bsocket.close();
                        bsocket = null;
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                    e.printStackTrace();
                }
                // 顯示連接結果
                bdevice.setText(sdname);
            }
        });
        // 彈出顯示AlertDialog
        ab.create().show();
    }


六、定義用於接收藍牙消息的線程

    public class ReceiveThread extends Thread {
        BluetoothSocket tsocket;<span style="white-space:pre">	</span>// 線程內部藍牙socket,局部變量

        public ReceiveThread(BluetoothSocket socket) {
            this.tsocket = socket;
            InputStream tistream = null;

            try {
                tistream = socket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            istream = tistream;
        }

        @Override
        public void run() {
            byte[] buffer = new byte[32];
            String str;
            // 線程一直運行
            while (true) {
                try {
                    istream.read(buffer);
                    str = new String(buffer,"UTF-8");
                    buffer = new byte[32];
                    Message msg = hd.obtainMessage(112,str);<span style="white-space:pre">	</span>// 把接收到的消息發送給Handler
                    hd.sendMessage(msg);
                } catch (IOException e) {
                    try {
                        if (istream != null) {
                            istream.close();
                        }
                        break;
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(50);<span style="white-space:pre">	</span>// 線程休眠,休眠時長影響線程每次可接收的數據量,數據越長,需要的時間越長
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


七、發送藍牙消息
    1、傳入字符串參數

    public void SendStr(String str) {
        byte[] bf = new byte[33];<span style="white-space:pre">	</span>// 按需字節數組初始化長度
        bf = str.getBytes();
        if((!str.equals("")) && (bsocket!=null)) {
            try {
                ostream = bsocket.getOutputStream();
                ostream.write(bf);
                ostream.write('\0');    // 發送一個結束標誌符
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ostream != null) {
                    ostream.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    2、傳入字節數組參數

    public static void SendBytes(byte[] bytes) {
        if(bytes.length>0 && (bsocket!=null)) {
            try {
                ostream = bsocket.getOutputStream();
                ostream.write(bytes);
                ostream.write('\0');
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ostream != null) {
                    ostream.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

八、退出應用

    先關閉藍牙socket,再關閉藍牙功能。注意捕獲異常。

    // 退出按鈕的點擊事件
    public void ExitOnClick(View Source) {
        if(bsocket != null)
            try {
                bsocket.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        // Close Bluetooth
        ba.disable();
        MainActivity.this.finish();
    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章