標籤:成功 對象 藍芽技術 優先順序 star finish 之間 intent ext
什麼是藍芽?
也可以說是藍芽技術。所謂藍芽(Bluetooth)技術,實際上是一種短距離無線電技術,是由愛立信公司公司發明的。利用“藍芽”技術,能夠有效地簡化掌上型電腦、膝上型電腦和行動電話手機等移動通訊終端裝置之間的通訊,也能夠成功地簡化以上這些裝置與網際網路Internet之間的通訊,從而使這些現代通訊裝置與網際網路之間的資料轉送變得更加迅速高效,為無線通訊拓寬道路。
Android 4.3(API Level 18)開始引入Bluetooth Low Energy(BLE,低功耗藍芽)的核心功能並提供了相應的 API, 應用程式通過這些 API 掃描藍牙裝置、查詢 services、讀寫裝置的 characteristics(屬性特徵)等操作。
Android BLE 使用的藍芽協議是 GATT 協議,有關該協議的詳細內容可以參見藍芽官方文檔。以下我引用一張官網的圖來大概說明 Android 開發中我們需要瞭解的一些 Bluetooth Low Energy 的專業術語。
Android提供BluetoothAdapter類藍芽通訊。通過調用建立的對象的靜態方法getDefaultAdapter()。其文法如下給出。
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
首先需要AndroidManifest.xml檔案中添加操作藍芽的許可權。
<!--需要此許可權來執行任何藍芽通訊,如請求一個串連、接受一個串連和傳輸資料。--> <uses-permission android:name="android.permission.BLUETOOTH"/> <!-- //如果你想讓你的應用啟動裝置發現或操縱藍芽設定,必須申報bluetooth_admin許可--> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
驗證藍芽是否開啟,未開啟的提示開啟
if (!mBluetoothAdapter.isEnabled()){ //彈出對話方塊提示使用者是後開啟 Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enabler, REQUEST_ENABLE); }
Activity代碼:
/** * Created by zhangqie on 2017/11/28. */public class BluetoothActivity extends AppCompatActivity implements View.OnClickListener{ private static final int REQUEST_ENABLE = 1; private static final String TAG = Demo1Activity.class.getName(); BluetoothAdapter mBluetoothAdapter; TextView tvDevices; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo1); initView(); } private void initView(){ findViewById(R.id.btn1).setOnClickListener(this); tvDevices = (TextView) findViewById(R.id.textblue); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()){ //彈出對話方塊提示使用者是後開啟 Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enabler, REQUEST_ENABLE); //不做提示,直接開啟,不建議用下面的方法,有的手機會有問題。 // mBluetoothAdapter.enable(); } showBluetooth(); } private void startSearthBltDevice() { //如果當前在搜尋,就先取消搜尋 if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } //開啟搜尋 mBluetoothAdapter.startDiscovery(); } private void showBoradCast(){ // 設定廣播資訊過濾 IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND);//每搜尋到一個裝置就會發送一個該廣播 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//當全部搜尋完後發送該廣播 filter.setPriority(Integer.MAX_VALUE);//設定優先權 // 註冊藍芽搜尋廣播接收者,接收並處理搜尋結果 this.registerReceiver(receiver, filter); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn1: showBoradCast(); startSearthBltDevice(); break; } } //擷取已經配對的藍牙裝置 private void showBluetooth(){ Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { tvDevices.append(device.getName() + ":" + device.getAddress()); } } } /** * 定義廣播接收器 */ private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { Log.i(TAG, ":"+ device.getAddress()); tvDevices.append(device.getName() + ":"+ device.getAddress()); } } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { Toast.makeText(Demo1Activity.this,"已搜尋完成",Toast.LENGTH_LONG).show(); //已搜素完成 } } };}
得到:
android -------- 藍芽Bluetooth