Mo *_*del 12
首先,您需要确保启用蓝牙然后搜索不成对设备,然后使用设备地址,您将设备配对.
成功配对后,您需要连接到设备以及HSP或HFP配置文件.请注意,如果没有HSP(耳机配置文件)或HFP(免提配置文件)蓝牙应用程序,您将无法连接并将呼叫转接到耳机或扬声器.
我为您准备了步骤,您可以通过Google搜索每一步轻松找到更多详细信息.希望这会对你有所帮助.
UPDATE
我会尝试帮助你更多:你必须在"src"文件夹下添加一个名为android.bluetooth的新包,然后创建IBluetoothHeadset.aidl
使用以下代码:
package android.bluetooth;
import android.bluetooth.BluetoothDevice;
interface IBluetoothHeadset {
// Public API
boolean connect(in BluetoothDevice device); //Api 11 and above
boolean connectHeadset(in BluetoothDevice device); // Below Api 11
boolean disconnect(in BluetoothDevice device);
boolean disconnectHeadset(in BluetoothDevice device);
List getConnectedDevices();
List getDevicesMatchingConnectionStates(in int[] states);
int getConnectionState(in BluetoothDevice device);
int getState(in BluetoothDevice device);
boolean setPriority(in BluetoothDevice device, int priority);
int getPriority(in BluetoothDevice device);
boolean startVoiceRecognition(in BluetoothDevice device);
boolean stopVoiceRecognition(in BluetoothDevice device);
boolean isAudioConnected(in BluetoothDevice device);
boolean sendVendorSpecificResultCode(in BluetoothDevice device,
in String command,
in String arg);
// APIs that can be made public in future
int getBatteryUsageHint(in BluetoothDevice device);
// Internal functions, not be made public
boolean acceptIncomingConnect(in BluetoothDevice device);
boolean rejectIncomingConnect(in BluetoothDevice device);
int getAudioState(in BluetoothDevice device);
boolean isAudioOn();
boolean connectAudio();
boolean disconnectAudio();
boolean startScoUsingVirtualVoiceCall(in BluetoothDevice device);
boolean stopScoUsingVirtualVoiceCall(in BluetoothDevice device);
void phoneStateChanged(int numActive, int numHeld, int callState, String number, int type);
void clccResponse(int index, int direction, int status, int mode, boolean mpty,
String number, int type);
}
然后在你的活动中
BluetoothDevice DeviceToConnect;
IBluetoothHeadset ibth;
//IBluetoothHeadset instance used to connect and disconnect headset afterwards
// Create and Register BroadCastListener for Action "HEADSET_INTERFACE_CONNECTED"
// In Broadcast your code has to be something like that
// if(ibth != null) ibth.connect(DeviceToConnect);
//Then After Pairing DeviceToConnect;
Intent i = new Intent(IBluetoothHeadset.class.getName());
if (bindService(i, HSPConnection, Context.BIND_AUTO_CREATE)) {
} else {
Log.e("HSP FAILED", "Could not bind to Bluetooth HFP Service");
}
//Method for bind
public static ServiceConnection HSPConnection= new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ibth = IBluetoothHeadset.Stub.asInterface(service);
//ibth instance used to connect and disconnect headset afterwards
Intent intent = new Intent();
intent.setAction("HEADSET_INTERFACE_CONNECTED");
//same as the one we register earlier for broadcastreciever
ctx.sendBroadcast(intent);
//ctx is Instance of Context
}
@Override
public void onServiceDisconnected(ComponentName name) {
ibth=null;
}
};
更新2:
这些是您必须添加到广播接收器的过滤器.
连接蓝牙时ACL_CONNECTED信号,ACL_DISCONNECTED信号蓝牙断开
对于特定设备,您必须检查广播接收器中的意图/上下文
所以你的新接收器包括前面的看起来像这样:
BroadcastReceiver bcr = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//Device found
}
else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//Done searching
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
//Device is about to disconnect
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
//Device has disconnected add whatever way u want to be notified here
//e.g music-vibration-screen light
}else if("HEADSET_INTERFACE_CONNECTED".equals(action){
if(ibth != null) ibth.connect(DeviceToConnect);
}
}
};
我忘了添加你在Manifest中需要这两个权限:
(编辑:威海站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|