欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

android如何實(shí)現(xiàn)藍(lán)牙app-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)android如何實(shí)現(xiàn)藍(lán)牙app,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),孝昌企業(yè)網(wǎng)站建設(shè),孝昌品牌網(wǎng)站建設(shè),網(wǎng)站定制,孝昌網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,孝昌網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

android實(shí)現(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()");
  //檢查手機(jī)是否支持藍(lán)牙4.0
  if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    Toast.makeText(this, "手機(jī)不支持藍(lán)牙4.0", Toast.LENGTH_SHORT).show();
    finish();
  }
  //調(diào)用系統(tǒng)服務(wù)的方式,請(qǐng)求開啟藍(lán)牙
  bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  bluetoothAdapter = bluetoothManager.getAdapter();
  //開啟藍(lán)牙
  if (!bluetoothAdapter.isEnabled()) {
    bluetoothAdapter.enable();
  }
}

//開始藍(lán)牙掃描  掃描到一個(gè)添加一個(gè)
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 對(duì)應(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對(duì)應(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;
}

//注冊(cè)藍(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();
      }
    }
  }
}

//注冊(cè)燈光監(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;
  }
}

關(guān)于“android如何實(shí)現(xiàn)藍(lán)牙app”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

分享題目:android如何實(shí)現(xiàn)藍(lán)牙app-創(chuàng)新互聯(lián)
本文路徑:http://chinadenli.net/article24/dcjsce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)網(wǎng)站營銷、網(wǎng)站設(shè)計(jì)全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)
欧美国产日韩变态另类在线看| 区一区二区三中文字幕| 人妻熟女欲求不满一区二区| 嫩草国产福利视频一区二区| 在线亚洲成人中文字幕高清| 日韩欧美第一页在线观看| 中文精品人妻一区二区| 97人摸人人澡人人人超碰| 免费黄色一区二区三区| 色综合久久中文综合网| 91偷拍视频久久精品| 日韩精品免费一区三区| 国产精品亚洲精品亚洲| 日本午夜精品视频在线观看| 久一视频这里只有精品| 国产精品成人免费精品自在线观看| 国产黑人一区二区三区| 午夜精品一区二区三区国产| 一区二区日韩欧美精品| 日韩在线欧美一区二区| 深夜视频在线观看免费你懂 | 麻豆tv传媒在线观看| 一区二区三区四区亚洲另类| 国产一区国产二区在线视频| 色婷婷日本视频在线观看| 欧美黑人暴力猛交精品| 亚洲综合伊人五月天中文| 日本人妻中出在线观看| 91欧美亚洲精品在线观看| 免费大片黄在线观看日本| 亚洲午夜av久久久精品| 大香蕉久久精品一区二区字幕| 亚洲av日韩一区二区三区四区| 欧美日韩在线观看自拍| 老司机精品线观看86| 91偷拍视频久久精品| 日本人妻的诱惑在线观看| 精品国产亚洲av成人一区| 人妻熟女中文字幕在线| 一本色道久久综合狠狠躁| 亚洲一区二区三区精选|