[置顶] 安卓蓝牙篇-适合入门

1.什么是蓝牙
  Bluetooth是目前使用的最广泛的无线通讯协议之一
  主要针对短距离设备通讯(10米)
  常用于连接耳机、鼠标和移动通讯设备等
 
2.发现周围蓝牙设备
  BluetoothAdapter:代表了本地的蓝牙适配器
  BluetoothDevice:代表一个远程的蓝牙设备
  
  扫描已配对的蓝牙设备方法:
  1.在AndroidManifest.xml声明蓝牙权限
  <uses-permission android:name="android.permission.BLUETOOTH" />
  
  2.获得BluetoothAdapter对象
  3.判断当前设备中是否拥有蓝牙设备
  4.判断当前设备中蓝牙设备是否已经打开

  5.得到已经配对的蓝牙设备对象


下面是整个完整的代码

package com.example.second;


import java.util.Iterator;
import java.util.Set;


import android.os.Bundle;
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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
private Button open;
private Button find;
private TextView Tv;
private TextView Tv1;
private BluetoothAdapter adapter;
private BluetoothDevice device;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        open=(Button)findViewById(R.id.open_bls);
        find=(Button)findViewById(R.id.find_bls);
        Tv=(TextView)findViewById(R.id.textView1);
        Tv1=(TextView)findViewById(R.id.textView2);
        adapter=BluetoothAdapter.getDefaultAdapter(); 
      //1.注册一个广播,用于接收“发现设备”的广播
        IntentFilter intentFilter =new IntentFilter(device.ACTION_FOUND);
        BluetoothReceiver receiver = new BluetoothReceiver();
        registerReceiver(receiver, intentFilter);
        open.setOnClickListener(new openbls());
        find.setOnClickListener(new findbls());
    }






private class openbls implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(adapter!=null){
if(adapter.isEnabled()){
Intent enable=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enable, 0x1);
//修改蓝牙设备的可见性
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); //可见持续300秒,不能超过300秒  
startActivity(intent); 
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if(devices.size()>0){
Iterator iterator=devices.iterator();
while(iterator.hasNext())
        device=(BluetoothDevice)iterator.next();  
       Tv1.setText(device.getName()+": "+device.getAddress());
     
}else{Tv1.setText("没有绑定的蓝牙设备。。。");}
}else{Toast.makeText(MainActivity.this,"蓝牙设备没有启动。。。", Toast.LENGTH_LONG).show();}
}else{Toast.makeText(MainActivity.this,"无蓝牙设备", Toast.LENGTH_LONG).show();}
}
}
private class findbls implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
adapter.startDiscovery();
Toast.makeText(MainActivity.this,"正在收索周围蓝牙设备", Toast.LENGTH_LONG).show();

}
}
private class  BluetoothReceiver extends BroadcastReceiver{


@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName()+": "+device.getAddress());
Tv.setText(device.getName()+": "+device.getAddress()+"\n");
}

}
    
}

你可能感兴趣的:(android,蓝牙)