蓝牙
来源:互联网 发布:betterzip mac版 编辑:程序博客网 时间:2024/06/10 09:25
public class MainActivity extends AppCompatActivity {
@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);
}
@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);
}
}
//权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
//允许程序发现和配对蓝牙设备
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
阅读全文
0 0
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- Annu. Rev. Genet:植物微生物组——系统性见解与展望
- Codeforces Round #432 (Div. 2) D
- 关于Android兼容7.0系统版本的问题
- 算法入门1:二分查找
- C语言实现栈(基于数组)
- 蓝牙
- 获取Android key hash值和 Sha1key 值
- 解决虚拟机下Ubuntu cheese黑屏问题
- 系统运行环境分析
- 切切切
- 关于mysql如何判断会不会产生死锁的分析
- LeetCode
- mybatis中关于传入参数parameterType
- Redis主从复制和集群配置 http://blog.csdn.net/u011204847/article/details/51307044