国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android系統中的藍牙連接程序編寫實例教程

2019-12-12 06:27:30
字體:
來源:轉載
供稿:網友

Bluetooth結構
1、JAVA層
frameworks/base/core/java/android/bluetooth/
包含了bluetooth的JAVA類。
2、JNI層
frameworks/base/core/jni/android_bluetooth_開頭的文件
定義了bluez通過JNI到上層的接口。
frameworks/base/core/jni/android_server_bluetoothservice.cpp
調用硬件適配層的接口system/bluetooth/bluedroid/bluetooth.c
3、bluez庫
external/bluez/
這是bluez用戶空間的庫,開源的bluetooth代碼,包括很多協議,生成libbluetooth.so。
4、硬件適配層
system/bluetooth/bluedroid/bluetooth.c
包含了對硬件操作的接口
system/bluetooth/data/*
一些配置文件,復制到/etc/bluetooth/。
還有其他一些測試代碼和工具。


簡略介紹Bluetooth開發使用到的類

1、BluetoothAdapter,藍牙適配器,可判斷藍牙設備是否可用等功能。
常用方法列舉如下:
    cancelDiscovery() ,取消搜索過程,在進行藍牙設備搜索時,如果調用該方法會停止搜索。(搜索過程會持續12秒)
    disable()關閉藍牙,也就是我們常說的禁用藍牙。
    enable()打開藍牙,這個方法打開藍牙但不會彈出提示,正常流程操作下,我們會讓系統提示用戶是否打開藍牙設備。如下兩行代碼可輕松搞定。

    Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enabler,reCode);//同startActivity(enabler);(在主Activity啟動一個二級Activity,reCode一般等于3,一定記得要在AndroidManifest.xml里面添加藍牙權限)
    getAddress()獲取本地藍牙地址
    getDefaultAdapter()獲取默認BluetoothAdapter,實際上,也只有這一種方法獲取BluetoothAdapter
    getName()獲取本地藍牙名稱
    getRemoteDevice(String address)根據藍牙地址獲取遠程藍牙設備
    getState()獲取本地藍牙適配器當前狀態(感覺可能調試的時候更需要)
    isDiscovering()判斷當前是否正在查找設備,是返回true
    isEnabled()判斷藍牙是否打開,已打開返回true,否則,返回false
    listenUsingRfcommWithServiceRecord(String name,UUID uuid)根據名稱,UUID創建并返回   
    BluetoothServerSocket,這是創建BluetoothSocket服務器端的第一步
    startDiscovery()開始搜索,這是搜索的第一步

 2.BluetoothDevice看名字就知道,這個類描述了一個藍牙設備
    createRfcommSocketToServiceRecord(UUIDuuid)根據UUID創建并返回一個BluetoothSocket
    這個方法也是我們獲取BluetoothDevice的目的――創建BluetoothSocket
    這個類其他的方法,如getAddress(),getName(),同BluetoothAdapter
    這個類有幾個隱藏方法,涉及到藍牙的自動配對,setPin,createBond,cancelPairingUserInput,等方法(需要通過java的反射,調用這幾個隱藏方法)

3.BluetoothServerSocket如果去除了Bluetooth相信大家一定再熟悉不過了,既然是Socket,方法就應該都差不多,
    這個類一種只有三個方法
    兩個重載的accept(),accept(inttimeout)兩者的區別在于后面的方法指定了過時時間,需要注意的是,執行這兩個方法的時候,直到接收到了客戶端的請求(或是過期之后),都會阻塞線程,應該放在新線程里運行!
    還有一點需要注意的是,這兩個方法都返回一個BluetoothSocket,最后的連接也是服務器端與客戶端的兩個BluetoothSocket的連接
    close()關閉!

4.BluetoothSocket,跟BluetoothServerSocket相對,是客戶端
    一共5個方法,不出意外,都會用到
    close(),關閉
    connect()連接
    getInptuStream()獲取輸入流
    getOutputStream()獲取輸出流
    getRemoteDevice()獲取遠程設備,這里指的是獲取bluetoothSocket指定連接的那個遠程藍牙設備

二、藍牙設備的發現、查找。

1.基于安全性考慮,設置開啟可被搜索后,Android系統會默認給出120秒的時間,其他設備能在這120秒內搜索到它。
    Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(enalbe,REQUEST_DISCOVERABLE);
2.搜索藍牙設備
    BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
    _bluetooth.startDiscovery();
3.關閉藍牙設備
    BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
    _bluetooth.disable();
4.創建藍牙客戶端
    BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString(UUID號));
    socket.connect();
4.創建藍牙服務端
    BluetoothServerSocket _serverSocket = _bluetooth.listenUsingRfcommWithServiceRecord(服務端名稱,UUID.fromeString(UUID號));
    BluetoothSocket socket = _serverSocket.accept();
    InputStream inputStream = socket.getInputStream();


TCP/IP通信相關代碼實現
注:tcp_ip模塊需要在系統setting模塊中,完成藍牙的配對,只有配對成功的,才能進行socket通信(具體如何配對和如何自動配對,將在bluetoot3或者4中進行講解)
AndridManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.thecaseforbluetooth" android:versionCode="1" android:versionName="1.0" >  <uses-sdk  android:minSdkVersion="8"  android:targetSdkVersion="15" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><uses-permission android:name="android.permission.BLUETOOTH" /> <application  android:icon="@drawable/ic_launcher"  android:label="@string/app_name"  android:theme="@style/AppTheme" >  <activity   android:name=".BluetoothActivity"   android:label="@string/title_activity_bluetooth" >   <intent-filter>    <action android:name="android.intent.action.MAIN" />     <category android:name="android.intent.category.LAUNCHER" />   </intent-filter>  </activity> </application> </manifest>

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnSearch" android:text="查找設備"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnExit" android:text="退出應用"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnDis" android:text="設置可被發現"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnserver" android:text="啟動服務端"/><ToggleButtonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tbtnSwitch" android:text="開/關 藍牙設備"/><ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/lvDevices" /></LinearLayout>

BluetoothActivity.java

package com.example.thecaseforbluetooth; import java.util.ArrayList;import java.util.Set; import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.Toast;import android.widget.ToggleButton; public class BluetoothActivity extends Activity { public Button searchBtn;//搜索藍牙設備 public Button exitBtn;//退出應用 public Button discoverBtn;//設置可被發現 public ToggleButton openBtn;//開關藍牙設備 public Button serverbtn; public ListView listView;//藍牙設備清單 public ArrayAdapter<String> adapter; public ArrayList<String> list =new ArrayList<String>(); private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> bondDevices ; public Context context ; @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  searchBtn = (Button)findViewById(R.id.btnSearch);  exitBtn = (Button)findViewById(R.id.btnExit);  discoverBtn = (Button)findViewById(R.id.btnDis);  openBtn = (ToggleButton)findViewById(R.id.tbtnSwitch);  serverbtn = (Button)findViewById(R.id.btnserver);  listView = (ListView)findViewById(R.id.lvDevices);  context = getApplicationContext();  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);  listView.setAdapter(adapter);  openBtn.setChecked(false);  //注冊廣播接收信號  IntentFilter intent = new IntentFilter();   intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver來取得搜索結果   intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); //每當掃描模式變化的時候,應用程序可以為通過ACTION_SCAN_MODE_CHANGED值來監聽全局的消息通知。比如,當設備停止被搜尋以后,該消息可以被系統通知

主站蜘蛛池模板:
女性|
禄劝|
舞钢市|
建阳市|
会宁县|
永宁县|
连平县|
罗山县|
澜沧|
瑞金市|
正蓝旗|
孟津县|
叶城县|
突泉县|
桦甸市|
漳平市|
浙江省|
勃利县|
巩义市|
苍山县|
巨野县|
南皮县|
墨江|
金乡县|
遂昌县|
察雅县|
潼南县|
宿迁市|
汕头市|
牡丹江市|
博乐市|
宁晋县|
略阳县|
双城市|
青浦区|
化隆|
永德县|
东兴市|
江西省|
综艺|
罗平县|