android笔记 蓝牙操作2
来源:互联网 发布:前海开源人工智能混合 编辑:程序博客网 时间:2024/05/23 21:01
关于设置蓝牙的可见性和扫描周围蓝牙设备的方法,需要用到广播接收器
package tjj.bluetooth2;import android.app.Activity;import 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.view.View;import android.widget.Button;public class TestBlueTooth2Activity extends Activity{private BluetoothAdapter bluetoothadapter;private Button scanbutton;private Button discoverbutton;public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);discoverbutton = (Button) findViewById(R.id.button1);scanbutton = (Button) findViewById(R.id.button2);discoverbutton.setOnClickListener(DiscoverButtonListener);scanbutton.setOnClickListener(ScanButtonListener);//得到一个代表本地蓝牙设备的bluetoothadapterbluetoothadapter = BluetoothAdapter.getDefaultAdapter();//设置一个过滤器,将action设置为BluetoothDevice.ACTION_FOUNDIntentFilter intentfilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);//得到一个关于蓝牙的广播接收器BluetoothReceiver bluetoothreceiver = new BluetoothReceiver();//将广播接收器和过滤器注册起来registerReceiver(bluetoothreceiver, intentfilter);}private Button.OnClickListener DiscoverButtonListener = new Button.OnClickListener(){public void onClick(View v){//設置可見性Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//设置可见时间discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);//打开的Activity是android自带的一个startActivity(discoverableIntent);}};private Button.OnClickListener ScanButtonListener = new Button.OnClickListener(){public void onClick(View v){//开始扫描周围的蓝牙设备,一次最少12秒,每扫描到一个蓝牙设备,就会发送一个广播,因此需要一个过滤器和广播接收器bluetoothadapter.startDiscovery();}};}class BluetoothReceiver extends BroadcastReceiver{public void onReceive(Context context, Intent intent){//能够通过这个过滤器的,它的action一定为我们需要的BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);System.out.println(device.getAddress());}}
- android笔记 蓝牙操作2
- android笔记 蓝牙操作1
- Android蓝牙操作2
- android蓝牙基本操作(个人笔记)
- Android 蓝牙操作(待续)
- Android 蓝牙操作
- Android之蓝牙操作
- android蓝牙操作一
- Android蓝牙操作
- Android蓝牙操作
- Android操作蓝牙
- Android蓝牙操作
- Android蓝牙操作
- Android开发--蓝牙操作
- android蓝牙操作
- Android蓝牙操作
- Android蓝牙操作
- Android蓝牙操作
- 最长上升子序列(LIS.pas/c/cpp)
- DFS.枚舉::poj1753 flip game && poj2965 the Pilots Brothers'refrigerator
- IT初学者答疑
- 连接字串
- Google Maps JavaScript API V3 根据地址 加载地图
- android笔记 蓝牙操作2
- quickSort学习
- 个人实现的大数模板(加、乘)
- c#使用开源控件读卡excel
- hdu 3829(二分图 有待复习)
- 计算机是如何启动的 How Computers Boot Up
- jdk环境变量配置
- printf与scanf
- MySql忘记root密码怎么解决