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);
}
}
android7.0开启蓝牙搜索设备
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本人是学艺不精的研究生一枚,由于项目用到了蓝牙功能,具体是需要让手机蓝牙和单片机通信,手机通过蓝牙发送指令给单片机...
- 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟,这篇我会把基本上...
- 微信小程序平台为帮助开发者提供更加丰富的服务,小程序与3月28日新增5个能力: 易小程(微信号:yixiaoche...