First of all, you need to make Sure Bluetooth turned on, then search for unpaired devices, and then use the device address. You are connecting a device.
After a successful connection, you will need to connect to the device, as well as to the HSP or HFP profiles. Note without HSP (headset profile) or HFP (Hands-Free profile), you cannot connect and transfer calls to the headset or speaker.
, .
, .
UPDATE
:
"src" : android.bluetooth
IBluetoothHeadset.aidl
:
package android.bluetooth;
import android.bluetooth.BluetoothDevice;
interface IBluetoothHeadset {
boolean connect(in BluetoothDevice device);
boolean connectHeadset(in BluetoothDevice device);
boolean disconnect(in BluetoothDevice device);
boolean disconnectHeadset(in BluetoothDevice device);
List<BluetoothDevice> getConnectedDevices();
List<BluetoothDevice> 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);
int getBatteryUsageHint(in BluetoothDevice device);
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;
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");
}
public static ServiceConnection HSPConnection= new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ibth = IBluetoothHeadset.Stub.asInterface(service);
Intent intent = new Intent();
intent.setAction("HEADSET_INTERFACE_CONNECTED");
ctx.sendBroadcast(intent);
}
@Override
public void onServiceDisconnected(ComponentName name) {
ibth=null;
}
};
2:
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
, .
ACL_CONNECTED Bluetooth ACL_DISCONNECTED Bluetooth
intents/context
, , , :
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)) {
}
else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
}else if("HEADSET_INTERFACE_CONNECTED".equals(action){
if(ibth != null) ibth.connect(DeviceToConnect);
}
}
};
, 2 :
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />