欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android實現(xiàn)藍(lán)牙app代碼

 更新時間:2018年05月29日 08:22:52   作者:tangzheng828  
這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)藍(lán)牙app的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了android實現(xiàn)藍(lán)牙app的具體代碼,供大家參考,具體內(nèi)容如下

private BluetoothGatt bluetoothGatt;
private BluetoothGattService gattService;
private BluetoothGattCharacteristic gattCharacteristic;
private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
private List<BluetoothDevice> devices = new ArrayList<>();

private UUID serviceUUID;  //不同設(shè)備 不同uuid
private UUID characteristicUUID;   //不同設(shè)備 不同uuid
private final UUID service4UUID= UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");  
private final UUID charAUUID = UUID.fromString("0000fffa-0000-1000-8000-00805f9b34fb");

private LightReceiver lightReceiver;
private ScanReceiver scanReceiver;
private ListView listView;
private TextView tvrefresh;
private String lightAction;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Log.i("tag", "MainActivity  onCreate()");
  //
  listView = (ListView) findViewById(R.id.lv_bluetooth);
  tvrefresh = (TextView) findViewById(R.id.tv_refresh_bluetooth);
  tvrefresh.setOnClickListener(this);
  openBluetooth();
  registeLigthReceiver();
  registeScanReceiver();

}

@Override
protected void onStart() {
  super.onStart();
  Log.i("tag", "MainActivity  onStart()");
  bluetoothScan();
}

//返回
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
  Log.i("tag", "MainActivity  onKeyUp()");
  this.finish();
  return super.onKeyUp(keyCode, event);
}

//重新掃描藍(lán)牙
@Override
public void onClick(View view) {
  switch (view.getId()) {
    case R.id.tv_refresh_bluetooth:
      //藍(lán)牙掃描
      bluetoothScan();
      break;
    default:
      break;
  }
}

//打開本地藍(lán)牙
private void openBluetooth() {
  Log.i("tag", "openLocalBluetooth()");
  //檢查手機是否支持藍(lán)牙4.0
  if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    Toast.makeText(this, "手機不支持藍(lán)牙4.0", Toast.LENGTH_SHORT).show();
    finish();
  }
  //調(diào)用系統(tǒng)服務(wù)的方式,請求開啟藍(lán)牙
  bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  bluetoothAdapter = bluetoothManager.getAdapter();
  //開啟藍(lán)牙
  if (!bluetoothAdapter.isEnabled()) {
    bluetoothAdapter.enable();
  }
}

//開始藍(lán)牙掃描  掃描到一個添加一個
private void bluetoothScan() {
  Log.i("tag", "bluetoothScan()");
  if (bluetoothAdapter == null) {
    openBluetooth();
  }
  if (!bluetoothAdapter.isDiscovering()) {
    bluetoothAdapter.startDiscovery();  //回調(diào)
  } else {
    Toast.makeText(this, "掃描中..", Toast.LENGTH_SHORT).show();
  }
}

//更新藍(lán)牙列表中的數(shù)據(jù)
private void updateUi() {
  Log.i("tag", "updateUi()");
  if (devices != null && devices.size() > 0) {
    BlueAdapter adapter = new BlueAdapter(this, devices);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
  } else {
    Toast.makeText(this, "附近沒有藍(lán)牙", Toast.LENGTH_SHORT).show();
  }
}

//取得gatt 對應(yīng)的service
private BluetoothGattService getGattService(BluetoothGatt gatt, UUID serviceUUID) {
  List<BluetoothGattService> gattServices = gatt.getServices();
  for (BluetoothGattService gattService : gattServices) {
    if (gattService.getUuid().equals(serviceUUID)) {
      return gattService;
    }
  }
  return null;
}

//取得service對應(yīng)的characteristic
private BluetoothGattCharacteristic getGattCharacteristic(BluetoothGattService gattService, UUID characteristicUUID) {
  List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
  for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
    if (gattCharacteristic.getUuid().equals(characteristicUUID)) {
      return gattCharacteristic;
    }
  }
  return null;
}

//注冊藍(lán)牙掃描監(jiān)聽
private void registeScanReceiver() {
  Log.i("tag", "registeScanReceiver()");
  scanReceiver = new ScanReceiver();
  IntentFilter filter = new IntentFilter();
  filter.addAction(BluetoothDevice.ACTION_FOUND);
  filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
  registerReceiver(scanReceiver, filter);
}

//定義藍(lán)牙掃描監(jiān)聽類 添加device , 更新界面
class ScanReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.i("tag", " BluetoothReceiver / ScanReceiver onReceive()  action=" + intent.getAction());
    String scanAction = intent.getAction();
    if (scanAction.equals(BluetoothDevice.ACTION_FOUND)) {
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
      if (!devices.contains(device)) {
        devices.add(device);
        if (CacheUtils.getBlueDeviceString(MainActivity1.this, device.getAddress()).equals("")) {
          CacheUtils.putBlueDeviceString(MainActivity1.this, device.getAddress(), device.getName());
        }
        updateUi();
      }
    } else if (scanAction.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
      if (devices == null || devices.size() == 0) {
        Toast.makeText(MainActivity1.this, "附近沒有發(fā)現(xiàn)藍(lán)牙設(shè)備", Toast.LENGTH_SHORT).show();
      }
    }
  }
}

//注冊燈光監(jiān)聽
private void registeLigthReceiver() {
  Log.i("tag", "registeReceiver()");
  lightReceiver = new LightReceiver();
  IntentFilter filter = new IntentFilter();
  filter.addAction("openLight");
  filter.addAction("closeLight");
  registerReceiver(lightReceiver, filter);
}

//定義燈控監(jiān)聽類
class LightReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

    Log.i("tag", " BluetoothReceiver  /LightReceiver onReceive()  action=" + intent.getAction());
    //
    String address = intent.getStringExtra("bluetoothAddress");  //從adapter取得的數(shù)據(jù)
    lightAction = intent.getAction();
    // if()   不同設(shè)備  不同serviceUUID,不同的characteristicUUID 自己確定
    serviceUUID=service4UUID;
    characteristicUUID=charAUUID;

    if (bluetoothAdapter == null) {
      openBluetooth();
    }
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);  
    MyBluetoothGattCallback gattCallback = new MyBluetoothGattCallback();
    bluetoothGatt = device.connectGatt(MainActivity1.this, false, gattCallback);  //回調(diào)

  }
}

//藍(lán)牙連接/通信回調(diào)
class MyBluetoothGattCallback extends android.bluetooth.BluetoothGattCallback {
  @Override
  public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    super.onConnectionStateChange(gatt, status, newState);
    Log.i("tag", "MyBluetoothGattCallback  onConnectionStateChange()  newState=" + newState);
    if (newState == BluetoothProfile.STATE_CONNECTED) {
      gatt.discoverServices();       //連接成功,開始搜索服務(wù),一定要調(diào)用此方法,否則獲取不到服務(wù)
      Log.i("tag", "MyBluetoothGattCallback  STATE_CONNECTED  ");
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
      Log.i("tag", "MyBluetoothGattCallback  STATE_DISCONNECTED");
    }
  }

  @Override
  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);
    Log.i("tag", "MyBluetoothGattCallback  onServicesDiscovered() status=" + status);

    if (lightAction.equals("openLight") || lightAction.equals("closeLight")) {  //避免 不停更新
      if (gattService == null || gattCharacteristic == null || !serviceUUID.equals(service4UUID) || !characteristicUUID.equals(charAUUID)) {
        gattService = getGattService(gatt, serviceUUID);
        if (gattService != null) {
          gattCharacteristic = getGattCharacteristic(gattService, characteristicUUID);
          if (gattCharacteristic != null) {
            gatt.setCharacteristicNotification(gattCharacteristic, true);
            gatt.connect();
          }
        }
      }
      if (lightAction.equals("openLight")) {
        gattCharacteristic.setValue("openLight"); //這里自己設(shè)置 藍(lán)牙模組需要的數(shù)據(jù)
        gatt.writeCharacteristic(gattCharacteristic);
      } else if (lightAction.equals("closeLight")) {
        gattCharacteristic.setValue("closeLight"); //這里自己設(shè)置 藍(lán)牙模組需要的數(shù)據(jù)
        gatt.writeCharacteristic(gattCharacteristic);
      }
      lightAction = "";
      gatt.readRemoteRssi();
    }
  }
}

@Override
protected void onDestroy() {
  super.onDestroy();
  Log.i("tag", "onDestroy()");
  if (bluetoothAdapter != null) {
    bluetoothAdapter.disable();
    bluetoothAdapter = null;
  }
  if (bluetoothGatt != null) {
    bluetoothGatt.disconnect();
    bluetoothGatt.close();
    bluetoothGatt = null;
  }

  if (lightReceiver != null) {
    unregisterReceiver(lightReceiver);
    lightReceiver = null;
  }
  if (scanReceiver != null) {
    unregisterReceiver(scanReceiver);
    scanReceiver = null;
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中的Dalvik和ART詳解及區(qū)別分析

    Android中的Dalvik和ART詳解及區(qū)別分析

    小編通過這篇文章給大家整理了什么是Dalvik和ART,并進(jìn)行了區(qū)別的分析,下面一起來看看。
    2016-07-07
  • android使用webwiew載入頁面使用示例(Hybrid App開發(fā))

    android使用webwiew載入頁面使用示例(Hybrid App開發(fā))

    Hybrid App 融合 Web App 的原理就是嵌入一個WebView組件,可以在這個組件中載入頁面,相當(dāng)于內(nèi)嵌的瀏覽器,下面是使用示例
    2014-03-03
  • Android仿微信朋友圈點贊和評論功能

    Android仿微信朋友圈點贊和評論功能

    這篇文章主要為大家詳細(xì)介紹了Android仿微信朋友圈點贊和評論功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝

    詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝

    這篇文章主要介紹了詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android四大組件之broadcast廣播使用講解

    Android四大組件之broadcast廣播使用講解

    Android開發(fā)的四大組件分別是:活動(activity),用于表現(xiàn)功能;服務(wù)(service),后臺運行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個應(yīng)用中存儲和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫,本篇著重介紹廣播組件
    2022-12-12
  • Android UI設(shè)計之AlertDialog彈窗控件

    Android UI設(shè)計之AlertDialog彈窗控件

    這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計之AlertDialog彈窗控件的使用方法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android獲取手機通話記錄的方法

    Android獲取手機通話記錄的方法

    這篇文章主要為大家詳細(xì)介紹了Android獲取手機通話記錄的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android編程實現(xiàn)的短信編輯器功能示例

    Android編程實現(xiàn)的短信編輯器功能示例

    這篇文章主要介紹了Android編程實現(xiàn)的短信編輯器功能,涉及Android權(quán)限控制、界面布局及短信功能相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android自定義ImageView實現(xiàn)自動放大縮小動畫

    Android自定義ImageView實現(xiàn)自動放大縮小動畫

    這篇文章主要為大家詳細(xì)介紹了Android自定義ImageView實現(xiàn)自動放大縮小動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android 啟動模式詳細(xì)介紹

    Android 啟動模式詳細(xì)介紹

    這篇文章主要介紹了Android 啟動模式詳細(xì)介紹的相關(guān)資料,Activity一共有以下四種launchMode啟動模式,這里一一做詳解,需要的朋友可以參考下
    2016-12-12

最新評論