首頁 > 軟體

android藍芽簡單開發範例教學

2021-12-08 16:00:30

概述

前段時間學習了一些藍芽開發的知識,記錄一下Android中藍芽的簡單開發。下面是最重要的兩個類。

BluetoothAdapter : 藍芽介面卡,通過getDefaultAdapter ()去獲取一個範例,如果裝置不支援藍芽的話,返回的是一個null物件,通過它,可以開啟、關閉藍芽,掃描裝置、向指定裝置建立socket通道…

BluetoothDevice : 代表一個裝置物件,可以通過它獲取裝置的名字、地址、型別等,也可以建立匹配,建立socket通道等等。

1、許可權申請

<uses-permission android:name="android.permission.BLUETOOTH"/> 使用藍芽所需要的許可權
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 使用掃描和設定藍芽的許可權(申明這一個許可權必須申明上面一個許可權)

Android6以上版本,掃描其他藍芽還需要位置許可權

// Android 9 以下版本
<user-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
// Android 9 以上
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

2、開啟藍芽

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 如果裝置不支援藍芽
if (mBluetoothAdapter == null){
    return;
}
// 裝置支援藍芽功能,呼叫startActivityForResult去啟動藍芽
if (!mBluetoothAdapter.isEnabled()){
    startBlueTooth.launch(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
}

開啟藍芽功能是通過startActivity去啟動的,但是startActivity這個函數已經過期了,所以我使用官方推薦的Activity Result替代它

    ActivityResultLauncher<Intent> startBlueTooth = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result==null){
                        Toast.makeText(BlueToothActivity.this, "open failed", Toast.LENGTH_SHORT).show();
                    }else {
                        if (result.getResultCode() == RESULT_CANCELED){
                            Toast.makeText(BlueToothActivity.this,"使用者取消",Toast.LENGTH_SHORT);
                        }
                    }
                }
            });

3、接收藍芽狀態的改變

通過廣播去接收藍芽狀態的改變

class BluetoothStateChangeReceiver extends BroadcastReceiver{
    public int DEFAULT_VALUE_BLUETOOTH = 1000;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,DEFAULT_VALUE_BLUETOOTH);

            switch(state){
                case BluetoothAdapter.STATE_ON:
                    Log.d(TAG, "onReceive: open");
                    break;
                case BluetoothAdapter.STATE_OFF:
                    Log.d(TAG, "onReceive: off");
                    break;
                case BluetoothAdapter.STATE_TURNING_ON :
                    Log.d(TAG, "onReceive: 正在開啟");
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    Log.d(TAG, "onReceive: 正在關閉");
                    break;
            }
        }
    }
}

別忘了廣播的註冊和解註冊

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
stateReceiver = new BluetoothStateChangeReceiver() ;
registerReceiver(stateReceiver,filter);

4、掃描其他的裝置

同樣通過廣播接收,action是BluetoothDevice.ACTION_FOUND

    class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // 從intent物件中獲取藍芽裝置的資訊
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // 當發現新裝置不存在於配對列表中時新增
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    blueNames.add(device.getName()+"t"+device.getAddress());
                }
                blueAdpater.notifyDataSetChanged();
                Log.d(TAG, "onReceive: " + device.getName());
            }
        }
    }

動態註冊廣播

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver,filter);

開啟掃描

mBluetoothAdapter.startDiscovery();

5、藍芽配對

    public class BondReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                switch(device.getBondState()){
                    case BluetoothDevice.BOND_BONDED:
                        Log.d(TAG, "onReceive: 配對完成");
                        break;
                    case BluetoothDevice.BOND_BONDING:
                        Log.d(TAG, "onReceive: 正在配對");
                        break;
                    case BluetoothDevice.BOND_NONE:
                        Log.d(TAG, "onReceive: 取消配對");
                        break;
                }
            }
        }
    }

6、獲取已經配對的裝置

已經配對的裝置會被儲存起來,通過BluetoothAdpater直接獲取即可

Set<BluetoothDevice> paireDevices = mBluetoothAdapter.getBondedDevices();
if (paireDevices.size()>0){
                        for (BluetoothDevice pairedDevice : pairedDevices) {
                        blueNames.add(pairedDevice.getName()+"  "+pairedDevice.getAddress());
                        Log.d(TAG, "onClick: "+pairedDevice.getName());
                    }
}

7、連線裝置

想要在兩臺裝置之間建立連線,必須實現使用者端和伺服器端機制,他們之間使用通訊端機制進行連線,伺服器端開放伺服器通訊端,使用者端通過MAC地址向伺服器端發起連線。使用者端和伺服器端以不同的方式獲得BluetoothSocket,當用戶端和伺服器端在同一個RFCOMM通道上分別擁有已連線的BluetoothSocket時,將他們視為彼此已經連線,於是每臺裝置都獲得輸入和輸出流式傳輸,並開始傳輸資料。

連線技術

一種實現技術是自動將每臺裝置準備為一個伺服器,從而使每臺裝置開放一個服務通訊端並偵聽連線,在此情況下,任何一臺裝置都可以發起與另一臺裝置的連線並稱為使用者端。

伺服器

設定伺服器通訊端並接受連線,步驟依次如下

1、呼叫listenUsingRfcommWithServiceRecord()獲取一個BluetoothServerSocket, 該函數需要兩個引數,第一個是伺服器的名稱,自己取一個即可,第二個是UUID,用來對資訊做唯一性標識,我們可以從網上眾多UUID生成器中隨機的生成一個,然後使用UUID.fromString(String)初始化一個UUID。

2、通過accept()函數開始偵聽連線請求

只有遠端裝置傳送的連線請求中UUID與使用此通訊端註冊的UUID相匹配時伺服器才會接受請求,accept函數會返回已連線的BluetoothSocket

3、連線成功後呼叫close()關閉BluetoothSocket

 private class AcceptThread extends Thread{

        private final BluetoothServerSocket mmServerSocket;
        private String mSocketType;

        public AcceptThread(boolean secure){
            BluetoothServerSocket tmp = null;
            mSocketType = secure ? "secure" : "Insercure";
            try{
                if (secure){
                    tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,MY_UUID_SECURE);
                }else{
                    tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME_INSECURE,MY_UUID_INSECURE);
                }
            } catch (IOException e) {
                Log.e(TAG,"socket type"+ mSocketType + "listen() failed",e);
            }

            mmServerSocket = tmp;
        }

        @Override
        public void run() {
            Log.d(TAG, "Socket Type: " + mSocketType +
                    "BEGIN mAcceptThread" + this);
            setName("AcceptThread"+ mSocketType);

            BluetoothSocket socket = null;
            Log.d(TAG, "run: 開始監聽");
            while (true){
                try{
                    socket = mmServerSocket.accept();
                    Log.d("acceptThread", "run: 連線成功");
                    connected(socket,socket.getRemoteDevice(),mSocketType);
                } catch (IOException e) {
                    Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
                    break;
                }

            }
            Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
        }

        public void cancel() {
            Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
            try {
                mmServerSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);
            }
        }
    }

上面的secure和Insecure只是使用了不同的UUID而已。

使用者端

遠端裝置開啟監聽後,我們就發起向此裝置的連線,首先必須先獲得遠端裝置的BluetoothDevice物件,然後獲取BluetoothSocket發起連線。

基本步驟如下

1、使用BluetoothDevice通過呼叫createRfcommSocketToServiceRecord(UUID) 獲取 BluetoothSocket

2、通過connect發起連線

private class ConnectThread extends Thread{
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;
        private String mSocketType;

        public ConnectThread(BluetoothDevice device, boolean secure){
            mmDevice = device;
            BluetoothSocket tmp = null;
            mSocketType = secure ? "Secure" : "Insecure";
            try {
                if (secure){
                    tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
                }else {
                    tmp = device.createRfcommSocketToServiceRecord(MY_UUID_INSECURE);
                }
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
            }
            mmSocket = tmp;
        }

        @Override
        public void run() {
            Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
            setName("ConnectThred"+mSocketType);

            // 總是取消發現,因為它會減慢連線
            bluetoothAdapter.cancelDiscovery();
            // connect
            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mmSocket.connect();
                Log.d(TAG, "run: socket連線成功");
            } catch (IOException e) {
                // Close the socket
                Log.d(TAG, "run: 關閉socket");
                try {
                    mmSocket.close();
                } catch (IOException e2) {
                    Log.e(TAG, "unable to close() " + mSocketType +
                            " socket during connection failure", e2);
                }

                return;
            }

            connected(mmSocket,mmDevice,mSocketType);
        }

        public void cancel(){
            try{
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
            }
        }

    }

傳送資料

連線成功後,我們就可以通過socket傳送資料了,使用者端的Socket物件是BluetoothSocket, 伺服器端的socket是BluetoothServerSocket,特別注意不要混淆了。使用getInputStreamgetOutputStream分別獲取通過通訊端處理資料傳輸的InputStreamOutputStream。寫資料比較簡單,但是讀資料就需要一個單獨的執行緒一直監聽才行。

private class ConnectedThread extends Thread{
        private final BluetoothSocket mmSocket;
        private InputStream mmInStream;
        private OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket, String socketType) throws IOException {
            Log.d(TAG, "create ConnectedThread: " + socketType);
            mmSocket = socket;

            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            try{
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();

                if (socket != null){
                    tmpOut.write(new String("hello").getBytes());
                    Log.d(TAG, "ConnectedThread: socket不是null");
                }
            } catch (IOException e) {
                Log.e(TAG,"temp socket not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;

            // mmOutStream.write(new String("hello").getBytes());
        }

        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            while (true){
                try{
                    bytes = mmInStream.read(buffer);
                    // send the bytes to the ui Activity
                    String text = encodeByteToString(buffer,bytes);
                    Log.d(TAG, "run: 收到訊息:"+ text);
                    chatItems.add(text);
                    mHandler.sendMessage(mHandler.obtainMessage());
                } catch (IOException e) {
                    Log.d(TAG, "run: 沒有收到訊息");
                    e.printStackTrace();
                    break;
                }
            }
        }

        public String encodeByteToString(byte[] data,int length) {
            byte[] temp = new byte[length];
            for (int i = 0; i < length; i++) {
                temp[i] = data[i];
            }
            try {
                return new String(temp,"utf-8");
            } catch (UnsupportedEncodingException e) {
                return "";
            }
        }


        public void write(byte[] buffer){
            try{
                mmOutStream.write(buffer);

                // mHandler.obtainMessage(Constants.MESSAGE_WRITE,-1,-1,buffer).sendToTarget();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void cancel(){
            try{
                mmSocket.close();
                Log.d(TAG, "cancel: connectedThread");
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }

上面的例子我主要是學習官網上的藍芽聊天專案寫的程式碼,大家也可以直接看官網專案。從上面的例子中可知,接受到的資料流都是一些二進位制,要用到實際的專案中還需要進行一定的編碼和轉換。也就是自己編寫一些協定,學過socket程式設計的同學一定都懂,其實藍芽已經有很多的好用的協定了,就比如AVRCP(Audio Video Remote Control Profile),定義了藍芽裝置和audio/video控制功能通訊的特點和過程, 結合MediaSession 可以很容易的實現裝置音視訊控制。

到此這篇關於android藍芽簡單開發範例教學的文章就介紹到這了,更多相關android藍芽開發內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com