實現藍芽vs安卓開發

來源:互聯網
上載者:User

  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.2cto.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端的接受代碼:

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



摘自 迷途羊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.