LG android TV 蓝牙,android7.0开启蓝牙搜索设备

package com.ansen.massage.chair.activity;

import android.Manifest;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.pm.PackageManager;

import android.net.Uri;

import android.os.Build;

import android.os.Bundle;

import android.support.v7.app.AlertDialog;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.Button;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

import com.ansen.massage.chair.R;

import com.ansen.massage.chair.adapter.BluetoothListAdapter;

import com.ansen.massage.chair.base.MCBaseActivity;

import com.ansen.massage.chair.base.Presenter;

import com.ansen.massage.chair.form.BluetoothDeviceForm;

import com.ansen.massage.chair.iview.IBluetoothListView;

import com.ansen.massage.chair.presenter.BluetoothListPresenter;

import com.ansen.massage.chair.util.CacheUtils;

import com.ansen.massage.chair.util.Constant;

import com.ansen.massage.chair.util.Utils;

/**

* Created by garbage ansen

* Create Time 2017-04-19

*/

public class BluetoothListActivity extends MCBaseActivity implements IBluetoothListView {

private String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};

private final int PERMS_REQUEST_CODE = 200;

private final int BLUETOOTH_DISCOVERABLE_DURATION = 250;

private final int REQUEST_CODE_BLUETOOTH_ON = 1313;

private BluetoothListPresenter presenter;

private ListView listView;

private BluetoothListAdapter adapter;

private TextView tvSearchState;

private BluetoothAdapter mBtAdapter;

private TextView tv_massage_chair;

private String language;

private TextView tv_device;

private Button bt_refresh;

@Override

protected void onCreateContent(Bundle savedInstanceState) {

super.onCreateContent(savedInstanceState);

setContentView(R.layout.activity_bluetooth_list);

language = CacheUtils.getString(this, CacheUtils.FILE_NAME, "10");

tv_massage_chair = (TextView) findViewById(R.id.tv_massage_chair);

tv_device = (TextView) findViewById(R.id.tv_device);

bt_refresh = (Button) findViewById(R.id.bt_refresh);

bt_refresh.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

searchDevice();

}

});

switch ((Utils.hexString2Bytes(language)[0] & 0xF0) >> 4) {

case Constant.LANGUAGE_CHINESE:

tv_massage_chair.setText("按摩椅APP");

tv_device.setText("设备列表");

bt_refresh.setText("重新扫描");

break;

case Constant.LANGUAGE_ENGLISH:

tv_massage_chair.setText("Massage chair APP");

tv_device.setText("Device list");

bt_refresh.setText("Searching again");

break;

case Constant.LANGUAGE_KOREAN:

tv_massage_chair.setText("안마의자 APP");

tv_device.setText("설비목록");

bt_refresh.setText("재검색");

break;

}

tvSearchState = (TextView) findViewById(R.id.tv_search_state);

listView = (ListView) findViewById(R.id.listview);

listView.setAdapter(adapter = new BluetoothListAdapter(this, presenter));

listView.setOnItemClickListener(onItemClickListener);

mBtAdapter = BluetoothAdapter.getDefaultAdapter();

if (System.currentTimeMillis() > 1514563200000L) {

// Toast.makeText(this, "您的当前版本已过期,请下载更新", Toast.LENGTH_LONG).show();

new AlertDialog.Builder(BluetoothListActivity.this)

.setTitle("注意")

.setMessage("当前版本已过期,请下载更新")

.setNegativeButton("取消", null)

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Intent intent = new Intent();

intent.setAction("android.intent.action.VIEW");

Uri content_url = Uri.parse("http://116.62.218.136:10000/%E6%8C%89%E6%91%A9%E6%A4%85/%E6%8C%89%E6%91%A9%E6%A4%85.apk");

intent.setData(content_url);

startActivity(intent);

finish();

}

})

.setCancelable(false)

.show();

} else {

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

this.registerReceiver(mReceiver, filter);

// Register for broadcasts when discovery has finished

filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

this.registerReceiver(mReceiver, filter);

}

if (mBtAdapter == null) {//手机不支持蓝牙

showToast("您的手机不支持蓝牙");

return;//手机不支持蓝牙就啥也不用干了,关电脑睡觉去吧

}

if (!mBtAdapter.isEnabled()) {// 如果蓝牙还没有打开

//请求打开 Bluetooth

Intent requestBluetoothOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

//设置 Bluetooth 设备可以被其它 Bluetooth 设备扫描到

requestBluetoothOn.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

//设置 Bluetooth 设备可见时间

requestBluetoothOn.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BLUETOOTH_DISCOVERABLE_DURATION);

//请求开启 Bluetooth

this.startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON);

return;

}

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {//Android 6.0以上版本需要获取权限

requestPermissions(perms, PERMS_REQUEST_CODE);//请求权限

} else {

searchDevice();//搜索任务

}

}

private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id) {

BluetoothDevice bluetoothDevice = presenter.getItem(position);

BluetoothDeviceForm bluetoothDeviceForm = new BluetoothDeviceForm(bluetoothDevice.getAddress());

goTo(MainActivity.class, bluetoothDeviceForm);

finish();

}

};

@Override

public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

switch (permsRequestCode) {

case PERMS_REQUEST_CODE:

boolean storageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;

if (storageAccepted) {

searchDevice();//搜索任务

} else {

Log.i("MainActivity", "没有权限操作这个请求");

}

break;

}

}

private void searchDevice() {

if (mBtAdapter.isDiscovering()) {

mBtAdapter.cancelDiscovery();

}

Log.i("BluetoothListActivity", "扫描蓝牙中...");

mBtAdapter.startDiscovery();

presenter.getListInfo().clear();

bt_refresh.setVisibility(View.INVISIBLE);

switch ((Utils.hexString2Bytes(language)[0] & 0xF0) >> 4) {

case Constant.LANGUAGE_CHINESE:

tvSearchState.setText("扫描蓝牙中...");

break;

case Constant.LANGUAGE_ENGLISH:

tvSearchState.setText("Searching for bluetooth...");

break;

case Constant.LANGUAGE_KOREAN:

tvSearchState.setText("블루투스검색중...");

break;

}

}

@Override

protected Presenter getPresenter() {

if (presenter == null) {

presenter = new BluetoothListPresenter(this);

}

return presenter;

}

@Override

protected void onDestroy() {

super.onDestroy();

// Log.i("ansen","onDestroy");

if (mBtAdapter != null) {

mBtAdapter.cancelDiscovery();

}

this.unregisterReceiver(mReceiver);

}

private final BroadcastReceiver mReceiver = 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);

Log.i("MainActivity", "device name:" + device.getName() + " BondState:" + device.getBondState());

// && device.getName().toUpperCase().contains("MASSAGE")

// if (device.getBondState() != BluetoothDevice.BOND_BONDED) {

presenter.addBluetoothDevice(device);

adapter.notifyDataSetChanged();

// }

} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {//搜索完成广播

bt_refresh.setVisibility(View.VISIBLE);

switch ((Utils.hexString2Bytes(language)[0] & 0xF0) >> 4) {

case Constant.LANGUAGE_CHINESE:

bt_refresh.setText("重新扫描");

tvSearchState.setText("扫描蓝牙完成");

break;

case Constant.LANGUAGE_ENGLISH:

bt_refresh.setText("Searching again");

tvSearchState.setText("Scanning bluetooth");

break;

case Constant.LANGUAGE_KOREAN:

bt_refresh.setText("재검색");

tvSearchState.setText("청아를 스캔 하여 완성하다");

break;

}

}

}

};

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

Log.i("ansen", "requestCode:" + requestCode + " resultCode:" + resultCode);

if (requestCode == REQUEST_CODE_BLUETOOTH_ON && resultCode == 250) {//用户选择开启 Bluetooth

Log.i("ansen", "开启蓝牙成功");

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {//Android 6.0以上版本需要获取权限

requestPermissions(perms, PERMS_REQUEST_CODE);//请求权限

} else {

searchDevice();//搜索任务

}

}

super.onActivityResult(requestCode, resultCode, data);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值