android之蓝牙操作(二)

1、修改本蓝牙设备的可见性
2、扫描周围可用蓝牙设备

步骤:
1、在androidManifest.xml中添加以下代码
  <uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>


2、在布局文件main.xml中添加两个按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/discoverId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="设置可见性"
/>
<Button
android:id="@+id/scanId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始扫描"
/>
</LinearLayout>


3、Activity文件

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.view.View.OnClickListener;
import android.widget.Button;

public class TestBluetooth02Activity extends Activity {
/** Called when the activity is first created. */
private Button discoverButton = null;
private Button scanButton = null;
private BluetoothReceiver bluetoothReceiver = null;
private BluetoothAdapter bluetoothAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

discoverButton = (Button)findViewById(R.id.discoverId);
discoverButton.setOnClickListener(new DiscoverButtonListener());
//创建一个IntentFilter对象,将其action制定为BluetoothDevice.ACTION_FOUND
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//生成一个BluetoothReceive对象
bluetoothReceiver = new BluetoothReceiver();
//注册广播接收器
registerReceiver(bluetoothReceiver,intentFilter);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
scanButton = (Button)findViewById(R.id.scanId);
scanButton.setOnClickListener(new ScanButtonListener());
}
private class DiscoverButtonListener implements OnClickListener
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建一个Intent对象,并惊奇Action的值设置为BluetoothAdapter.ACTION_REQEST_DISCOVERABLE
Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//将一个键值对放在Intent对象中,主要用于指定可见状态的持续时间
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
startActivity(discoverIntent);
}


}
private class BluetoothReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//可以从收到的Intent对象当中,将代表远程的蓝牙适配器的对象取出
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
}
}


}
private class ScanButtonListener implements OnClickListener
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bluetoothAdapter.startDiscovery();
}

}
/* protected void onDestroy
{
unregisterReceiver(bluetoothReceiver);
super.onDestroy();
}*/
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值