Android Bluetooth打开关闭
2015-12-21 00:10
411 查看
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Toast.makeText(this, "不支持蓝牙", Toast.LENGTH_SHORT).show(); } else if (!mBluetoothAdapter.isEnabled()) { Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBTIntent, REQUEST_ENABLE_BT); }
private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0); switch (state) { case BluetoothAdapter.STATE_TURNING_ON: Toast.makeText(MainActivity.this, "Bluetooth正在打开", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_ON: Toast.makeText(MainActivity.this, "Bluetooth已打开", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_TURNING_OFF: Toast.makeText(MainActivity.this, "Bluetooth正在关闭", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_OFF: Toast.makeText(MainActivity.this, "Bluetooth已关闭", Toast.LENGTH_SHORT).show(); break; } } };
@Override protected void onResume() { super.onResume(); registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); } @Override protected void onStop() { super.onStop(); unregisterReceiver(receiver); }
相关文章推荐
- jquey id file
- Convert UIImage to CVPixelBufferRef
- 1.使用UISearchController制作一个简单的本地搜索应用
- CMSampleBufferRef 与 UIImage 的转换
- 蓝懿iOS全局局部变量/for循环/uilabel
- EasyUI-----给combobox添加OnSelect事件
- Confluence 5.4实现与JIRA前所未有的集成
- UESTC 第七届ACM趣味程序设计竞赛第四场(正式赛)
- UIView总结
- Android:Message、Handler、Message Queue与Looper
- UITableView---总结
- 第3章 UI
- 【实践】——PowerBuilder登录+PB自带数据库
- iOS中UISearchBar(搜索框)使用总结
- chapter23:软件安装RPM,SRPM与YUM之(2)-SRPM的使用:rpmbuild
- <%@ include file=”url”%> 和<jsp:include page=”url” flush=”true” /> 的区别
- Java:String、StringBuffer和StringBuilder的区别
- roguelike2d 摄像机参数设置
- UILabel中长文字自动换行方法
- mac下搭建quick