實現藍牙vs安卓開發

  android實現藍牙開發的資料不是很多,在安卓開發中,如何實現藍牙通信呢?一般的方法是通過藍牙的虛擬串口,然後經過一系列的配置完成簡單的實現,外置藍牙的通訊也是用這種方法來做的!但是我們在進行android開發的時候這種方法是不支持的,所以我就來演示一下關於藍牙安卓軟件開用發第三方.NET組件(inthehand)來將其實現。

接下來我們初始化核心代碼,作為客戶端的pc機它需要通過手機藍牙MAC地址實現通信!
/// <summary>
        /// 打開端口
        /// </summary>
        /// <param name="Name">端口名稱</param>
        /// <returns>成功否</returns>
        public bool OpenPort(string Name)
        {

            InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable;
            InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient();
            InTheHand.Net.Sockets.BluetoothDeviceInfo[] devices = cli.DiscoverDevices();
            foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo device in devices)//設備搜尋
            {
                device.Update();
                device.Refresh();
                MessageBox.Show("設備已找到");
                break;
            }

 

            BluetoothDeviceInfo bd = new BluetoothDeviceInfo(devices[0].DeviceAddress);
            bluetoothClient = new BluetoothClient();

            Guid mGUID = Guid.Parse("fa87c0d0-afac-11de-8a39-0800200c9a66");
            bluetoothClient.Connect(devices[0].DeviceAddress, mGUID);//客戶端對地址實現連接,這是一個阻塞線程,需要服務器端的回應 www.aiwalls.com

            ReceiveThread = new Thread(ReceiveMethod);
            ReceiveThread.Start();

 

            return true;
        }
接下來用手機接受pc的鏈接請求:
 if (connected)
              {
                   return;
                }
              try
                 {
                     mBThServer = myBluetoothAdapter
                              .listenUsingRfcommWithServiceRecord(NAME_SECURE,
                                      MY_UUID_SECURE);
                 } catch (IOException e)
                 {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
 
                 try
                {
                    mBTHSocket = mBThServer.accept();
                 } catch (IOException e)
                 {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 try
                 {
                     mBufferedReader = new BufferedReader(new InputStreamReader(
                             mBTHSocket.getInputStream()));
                } catch (IOException e1)
                 {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();
                 }// 取得輸入、輸出流
                 try
                {
                     mPrintStream = new PrintStream(
                            mBTHSocket.getOutputStream(), true);
                     connected = true;
                 } catch (IOException e)
                {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
通過手機來發送數據:

接下來是pc端的接受代碼:

到這裡代碼基本完成,但是我們需要註意的是:
我們一定要註意庫函數的版本,版本不對是一件很麻煩的事情。
手機中的藍牙設備也一定要打開。
好瞭就到這裡!!!

摘自 迷途羊

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。