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    {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//创建一个Intent对象,并惊奇Action的值设置为BluetoothAdapter.ACTION_REQEST_DISCOVERABLEIntent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//将一个键值对放在Intent对象中,主要用于指定可见状态的持续时间discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);startActivity(discoverIntent);}            }    private class BluetoothReceiver extends BroadcastReceiver    {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubString 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    {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubbluetoothAdapter.startDiscovery();}        }  /*  protected void onDestroy    {    unregisterReceiver(bluetoothReceiver);    super.onDestroy();    }*/}

更多相关文章

  1. Android studio 3.1.3 无法获取pom文件
  2. android各个文件分析
  3. Android多国语言文件夹命名
  4. Android 自动编译、打包生成apk文件 4 - 多渠道批量打包
  5. 【Android 应用开发】Android资源文件 - 使用资源存储字符串 颜
  6. android 布局文件属性说明
  7. Android 属性文件build.prop,获取属性以及设置属性

随机推荐

  1. [Android]搜索界面--自动弹出键盘,并点击
  2. Android启动画面实现
  3. Android4: 旋转屏幕时禁止Activity重启
  4. Android02--debug.keystore的注册信息
  5. Android(安卓)Spinner
  6. Android(安卓)ApiDemos示例解析(193):Vie
  7. android常用开源库分享
  8. smack 源码分析- PacketWriter (android
  9. Android多媒体框架初步分析
  10. Android经典的大牛博客推荐