1.蓝牙的开启与查找 2.蓝牙通信:蓝牙聊天室
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hala.bluetoothchat"> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.hala.bluetoothchat.MainActivity"> <Button android:id="@+id/paired" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:text="已配对设备" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/scan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="扫描附近蓝牙设备" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/paired" /> </android.support.constraint.ConstraintLayout>MainActivity.java
package com.hala.bluetoothchat; 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.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.Set; public class MainActivity extends AppCompatActivity { public static final String TAG="BlueToothChat"; public static final int REQUEST_CODE = 10; private BluetoothAdapter bluetoothAdapter; private Button paired; private Button scan; private BroadcastReceiver mBluetoothReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action=intent.getAction(); Log.d(TAG,"Action:"+action); //如果已经连接上 if(action.equals(BluetoothDevice.ACTION_FOUND)){ BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d(TAG,"New Device name:"+device.getName()); Log.d(TAG,"New Device addr:"+device.getAddress()); //如果扫描完毕 }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ Log.d(TAG,"Distory Done!"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //判断设备是否支持蓝牙 bluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); if(bluetoothAdapter==null){ Log.e(TAG,"Device not support bluetooth"); }else { Toast.makeText(this, "设备支持蓝牙!", Toast.LENGTH_SHORT).show(); } paired=(Button)findViewById(R.id.paired); scan=(Button)findViewById(R.id.scan); //查找已配对设备,获取名称,地址 paired.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Set<BluetoothDevice> pairedDevice=bluetoothAdapter.getBondedDevices(); for(BluetoothDevice device:pairedDevice){ Log.d(TAG,"Device name:"+device.getName()); Log.d(TAG,"Device addr:"+device.getAddress()); } } }); scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //如果正在扫描,就停止扫描 if(bluetoothAdapter.isDiscovering()){ bluetoothAdapter.cancelDiscovery(); } //开始扫描 bluetoothAdapter.startDiscovery(); } }); IntentFilter filter=new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mBluetoothReceiver,filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mBluetoothReceiver); } @Override protected void onStart() { super.onStart(); if(!bluetoothAdapter.isEnabled()){ //向系统请求开启蓝牙 Intent enableIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_CODE); }else{ //已经开启蓝牙 Toast.makeText(this, "蓝牙已经开启了!", Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==REQUEST_CODE){ //已经开启蓝牙 Toast.makeText(this, "蓝牙已经开启了!", Toast.LENGTH_SHORT).show(); } } }显示结果
