ble_registering = false;
}
+ void OnConnectionState(int status, int client_id, const char* address,
+ bool connected) override {
+ //TODO(jpawlowski): implement
+ }
+
void OnScanResult(const bluetooth::ScanResult& scan_result) override {
if (showing_prompt.load())
cout << endl;
UnregisterAll();
return android::NO_ERROR;
}
+ case CONNECT_TRANSACTION: {
+ int client_id = data.readInt32();
+ const char* address = data.readCString();
+ bool is_direct = data.readBool();
+
+ bool result = Connect(client_id, address, is_direct);
+ reply->writeInt32(result);
+
+ return android::NO_ERROR;
+ }
+ case DISCONNECT_TRANSACTION: {
+ int client_id = data.readInt32();
+ const char* address = data.readCString();
+
+ bool result = Disconnect(client_id, address);
+ reply->writeInt32(result);
+
+ return android::NO_ERROR;
+ }
case START_SCAN_TRANSACTION: {
int client_id = data.readInt32();
auto settings = CreateScanSettingsFromParcel(data);
data, &reply);
}
+bool BpBluetoothLowEnergy::Connect(int client_id, const char* address,
+ bool is_direct) {
+ Parcel data, reply;
+
+ data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
+ data.writeInt32(client_id);
+ data.writeCString(address);
+ data.writeBool(is_direct);
+
+ remote()->transact(IBluetoothLowEnergy::CONNECT_TRANSACTION,
+ data, &reply);
+
+ return reply.readInt32();
+}
+
+bool BpBluetoothLowEnergy::Disconnect(int client_id, const char* address) {
+ Parcel data, reply;
+
+ data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
+ data.writeInt32(client_id);
+ data.writeCString(address);
+
+ remote()->transact(IBluetoothLowEnergy::DISCONNECT_TRANSACTION,
+ data, &reply);
+
+ return reply.readInt32();
+}
+
bool BpBluetoothLowEnergy::StartScan(
int client_id,
const bluetooth::ScanSettings& settings,
virtual void UnregisterClient(int client_if) = 0;
virtual void UnregisterAll() = 0;
+ virtual bool Connect(int client_id, const char* address, bool is_direct) = 0;
+ virtual bool Disconnect(int client_id, const char* address) = 0;
+
virtual bool StartScan(
int client_id,
const bluetooth::ScanSettings& settings,
const android::sp<IBluetoothLowEnergyCallback>& callback) override;
void UnregisterClient(int client_if) override;
void UnregisterAll() override;
+
+ bool Connect(int client_id, const char* address, bool is_direct) override;
+ bool Disconnect(int client_id, const char* address) override;
+
bool StartScan(
int client_id,
const bluetooth::ScanSettings& settings,
OnClientRegistered(status, client_if);
return android::NO_ERROR;
}
+ case ON_CONNECTION_STATE_TRANSACTION: {
+ int status = data.readInt32();
+ int client_id = data.readInt32();
+ const char* address = data.readCString();
+ bool connected = data.readBool();
+
+ OnConnectionState(status, client_id, address, connected);
+ return android::NO_ERROR;
+ }
case ON_SCAN_RESULT_TRANSACTION: {
auto scan_result = CreateScanResultFromParcel(data);
CHECK(scan_result.get());
IBinder::FLAG_ONEWAY);
}
+void BpBluetoothLowEnergyCallback::OnConnectionState(
+ int status, int client_id, const char* address, bool connected) {
+
+ Parcel data;
+
+ data.writeInterfaceToken(
+ IBluetoothLowEnergyCallback::getInterfaceDescriptor());
+ data.writeInt32(status);
+ data.writeInt32(client_id);
+ data.writeCString(address);
+ data.writeBool(connected);
+
+ remote()->transact(
+ IBluetoothLowEnergyCallback::ON_CONNECTION_STATE_TRANSACTION,
+ data, NULL,
+ IBinder::FLAG_ONEWAY);
+}
+
void BpBluetoothLowEnergyCallback::OnScanResult(
const bluetooth::ScanResult& scan_result) {
Parcel data, reply;
};
virtual void OnClientRegistered(int status, int client_if) = 0;
+ virtual void OnConnectionState(int status, int client_id, const char* address,
+ bool connected) = 0;
virtual void OnScanResult(const bluetooth::ScanResult& scan_result) = 0;
virtual void OnMultiAdvertiseCallback(
int status, bool is_start,
// IBluetoothLowEnergyCallback overrides:
void OnClientRegistered(int status, int client_if) override;
+ void OnConnectionState(int status, int client_id, const char* address,
+ bool connected) override;
void OnScanResult(const bluetooth::ScanResult& scan_result) override;
void OnMultiAdvertiseCallback(
int status, bool is_start,
*/
void unregisterAll();
+ /* Initiates a BLE connection do device with address |address|. If
+ * |is_direct| is set, use direct connect procedure. Return true on success,
+ * false otherwise.
+ */
+ boolean Connect(in int client_id, in const char* address,
+ in boolean is_direct);
+
+ /* Disconnect from previously connected BLE device with address |address|.
+ * Return true on success, false otherwise.
+ */
+ boolean Disconnect(in int client_id, in const char* address);
+
/**
* Initiates a BLE device scan for the scan client with ID |client_id|, using
* the parameters defined in |settings|. Scan results that are reported to the
*/
void onClientRegistered(in int status, in int client_if);
+ /* Called asynchronously to notify the delegate of connection state change.
+ */
+ void OnConnectionState(in int status, in int client_id, in const char* address,
+ in bool connected);
+
/**
* Called to report BLE device scan results once a scan session is started for
* this client using IBluetoothLowEnergy.startScan. |scan_result| contains all
UnregisterAllBase();
}
+bool BluetoothLowEnergyBinderServer::Connect(int client_id,
+ const char* address,
+ bool is_direct) {
+ VLOG(2) << __func__ << " client_id: " << client_id
+ << " address: " << address
+ << " is_direct: " << is_direct;
+ std::lock_guard<std::mutex> lock(*maps_lock());
+
+ auto client = GetLEClient(client_id);
+ if (!client) {
+ LOG(ERROR) << "Unknown client_id: " << client_id;
+ return false;
+ }
+
+ return client->Connect(std::string(address), is_direct);
+}
+
+bool BluetoothLowEnergyBinderServer::Disconnect(int client_id,
+ const char* address) {
+ VLOG(2) << __func__ << " client_id: " << client_id
+ << " address: " << address;
+ std::lock_guard<std::mutex> lock(*maps_lock());
+
+ auto client = GetLEClient(client_id);
+ if (!client) {
+ LOG(ERROR) << "Unknown client_id: " << client_id;
+ return false;
+ }
+
+ return client->Disconnect(std::string(address));
+}
+
bool BluetoothLowEnergyBinderServer::StartScan(
int client_id,
const bluetooth::ScanSettings& settings,
bluetooth::LowEnergyClient* client, int status,
const char* address, bool connected) {
VLOG(2) << __func__ << " address: " << address << " connected: " << connected;
+
+ int client_id = client->GetInstanceId();
+ auto cb = GetLECallback(client->GetInstanceId());
+ if (!cb.get()) {
+ VLOG(2) << "Client was unregistered - client_id: " << client_id;
+ return;
+ }
+
+ cb->OnConnectionState(status, client_id, address, connected);
}
void BluetoothLowEnergyBinderServer::OnScanResult(
const android::sp<IBluetoothLowEnergyCallback>& callback) override;
void UnregisterClient(int client_id) override;
void UnregisterAll() override;
+ bool Connect(int client_id, const char* address, bool is_direct) override;
+ bool Disconnect(int client_id, const char* address) override;
bool StartScan(
int client_id,
const bluetooth::ScanSettings& settings,