Bluetooth 蓝牙 操作
来源:互联网 发布:软件自动升级程序 编辑:程序博客网 时间:2024/06/05 01:52
//布局<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.a1511j.day02_.MainActivity"> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启蓝牙" tools:layout_editor_absoluteX="6dp" tools:layout_editor_absoluteY="3dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="120dp" android:layout_marginStart="120dp" android:layout_marginTop="40dp" /> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭蓝牙" tools:layout_editor_absoluteX="-3dp" tools:layout_editor_absoluteY="77dp" android:layout_below="@+id/btn_start" android:layout_alignLeft="@+id/btn_start" android:layout_alignStart="@+id/btn_start" android:layout_marginTop="58dp" /> <Button android:id="@+id/btn_discoverable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="可被发现蓝牙" tools:layout_editor_absoluteX="-3dp" tools:layout_editor_absoluteY="155dp" android:layout_below="@+id/btn_stop" android:layout_alignLeft="@+id/btn_stop" android:layout_alignStart="@+id/btn_stop" android:layout_marginTop="51dp" /> <Button android:id="@+id/btn_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索蓝牙" tools:layout_editor_absoluteX="6dp" tools:layout_editor_absoluteY="250dp" android:layout_marginLeft="17dp" android:layout_marginStart="17dp" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/btn_stop" android:layout_alignStart="@+id/btn_stop" android:layout_marginBottom="106dp" /></RelativeLayout>mianimport android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.Toast;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;public class MainActivity extends AppCompatActivity { //代替findviewByid; @BindView(R.id.btn_start) Button btnStart; @BindView(R.id.btn_stop) Button btnStop; @BindView(R.id.btn_discoverable) Button btnSearch; @BindView(R.id.btn_search) Button button4; private BluetoothAdapter bluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //获取蓝牙适配器 bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } @OnClick({R.id.btn_start, R.id.btn_stop, R.id.btn_search, R.id.btn_discoverable}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.btn_start: bluetoothAdapter.enable(); Toast.makeText(MainActivity.this,"蓝牙已打开",Toast.LENGTH_SHORT).show(); break; case R.id.btn_stop: bluetoothAdapter.disable(); Toast.makeText(MainActivity.this,"蓝牙已关闭",Toast.LENGTH_SHORT).show(); break; case R.id.btn_discoverable: discoverableBluetooth(); break; case R.id.btn_search: searchBluetooh(); break; } } /** * 打开蓝牙的可检测性 */ private void discoverableBluetooth() { Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(intent,0); Toast.makeText(MainActivity.this,"蓝牙可检测性已打开",Toast.LENGTH_SHORT).show(); } //搜索蓝牙 private void searchBluetooh() { //添加蓝牙可以被发现 IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); MBluetoothBroadCast mBluetoothBroadCast = new MBluetoothBroadCast(); registerReceiver(mBluetoothBroadCast,intentFilter); IntentFilter intentFilter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mBluetoothBroadCast,intentFilter1); //开始找蓝牙设备 bluetoothAdapter.startDiscovery(); } //符合我注册的过滤器 ,就可以进入onReceive方法 class MBluetoothBroadCast extends 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); if(device.getBondState()!=BluetoothDevice.BOND_BONDED){ //用设备获取蓝牙名字 System.out.println(device.getName()+",,,,,,,,,,,,,,,,"); } //如果搜索完毕,会进入这个判断; }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ Toast.makeText(MainActivity.this, "已搜数完成", Toast.LENGTH_LONG).show(); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); }}//开启蓝牙的依赖compile 'com.jakewharton:butterknife:8.8.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'//开启蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
阅读全文
0 0
- Bluetooth 蓝牙 操作
- Android Bluetooth蓝牙基本操作
- android Bluetooth的实现蓝牙操作
- Android之Bluetooth—蓝牙操作
- 安卓蓝牙(Bluetooth)之:本地蓝牙操作
- Bluetooth(蓝牙)
- BlueTooth: 蓝牙
- Bluetooth---蓝牙
- Bluetooth蓝牙
- 蓝牙Bluetooth
- BlueTooth----蓝牙
- 蓝牙BlueTooth
- Bluetooth蓝牙
- Bluetooth 蓝牙
- Bluetooth蓝牙
- BlueTooth: 什么是蓝牙(Bluetooth)
- Bluetooth--- android -- 蓝牙 bluetooth
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- 学习c程的一点点感想
- mysql外键约束解释说明
- leetcode 695 题解
- 用java写一个简单的计算器详解代码
- 11月5日
- Bluetooth 蓝牙 操作
- Trie
- mos驱动查错
- Combinatorics——HDUOJ 1261
- 关于FOR的用法
- 01背包
- druid和HikariCP
- 如何正确的根据自身喜好投简历
- 程序小白入门史