Android 蓝牙的使用(待续)
2016-02-23 10:44
731 查看
一、蓝牙的打开方式(两种)
方式一(授权打开):
[code]Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent);
方式二(静默打开):
[code] mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter.enable();
注意权限:
[code] <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
二、扫描设备
Demo1
MainActivity[code]public class BlueToothDemoOne extends Activity { private Button mButtonBluetooth; private TextView mTextViewDeviceBonded; private TextView mTextViewDeviceSearch; private BluetoothAdapter mBluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.bluetoothone); mButtonBluetooth = (Button) findViewById(R.id.buttonbluetooth); mTextViewDeviceBonded = (TextView) findViewById(R.id.textview_bonded); mTextViewDeviceSearch = (TextView) findViewById(R.id.textview_search); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 获取之前配对过的设备 Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices(); if (devices != null) { for (BluetoothDevice device : devices) { mTextViewDeviceBonded.append("" + device.getName() + ":" + device.getAddress()); } } mBluetoothAdapter.enable(); // 搜索到设备时发送广播 IntentFilter intentfilter = new IntentFilter( BluetoothDevice.ACTION_FOUND); this.registerReceiver(mReceiver, intentfilter); // 搜索完成时发送广播 intentfilter = new IntentFilter( BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(mReceiver, intentfilter); } public void click_start(View v) { // 正在搜索时点击按钮将停止搜索 if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); mButtonBluetooth.setText("开始搜索"); } else { // 开启搜索设备 mBluetoothAdapter.startDiscovery(); mButtonBluetooth.setText("正在搜索"); } } // 接收广播 private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //根据不同的Intent.getAction进行分类 if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) { //对搜索到的设备进行显示 BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { mTextViewDeviceSearch.append(device.getName() + ":" + device.getAddress() + "\n"); } } if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent .getAction())) { mTextViewDeviceSearch.append("搜索完毕"); mButtonBluetooth.setText("开始搜索"); } } }; }
布局:
[code]<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/buttonbluetooth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click_start" android:text="开始搜索" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="已配对的设备" /> <TextView android:id="@+id/textview_bonded" android:layout_width="match_parent" android:layout_height="wrap_content" /> <View android:layout_width="match_parent" android:layout_height="2px" android:background="#00f" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:text="搜索到的设备" /> <TextView android:id="@+id/textview_search" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
相关文章推荐
- Gradle:Plugin is too old,please update to a more recent version
- <<Android源码设计模式解析与实战>>读书笔记----- Android NDK开发学习
- Android 编程下 Touch 事件的分发和消费机制
- android.view.ViewConfiguration
- android使用自定义标题时提示“you cannot combine custom titles with other  ”
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令
- android笔试题
- 【Android】【Other】Android一些常用命令
- Android之设置闹钟
- Android View刷新机制实例分析
- Android UI效果之绘图篇(二)
- 阅读《Android 从入门到精通》(28)——使用 XML 的选项菜单
- Android 开发之旅:view的几种布局方式及实践
- Android&java的成长之路之六(2048小游戏②)