1 /* 2 * Copyright 2021 HIMSA II K/S - www.himsa.com. 3 * Represented by EHIMA - www.ehima.com 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.bluetooth.hap; 19 20 import static java.util.Objects.requireNonNull; 21 22 import android.bluetooth.BluetoothDevice; 23 24 import com.android.bluetooth.Utils; 25 26 import java.lang.annotation.Native; 27 28 /** Hearing Access Profile Client Native Interface to/from JNI. */ 29 public class HapClientNativeInterface { 30 private static final String TAG = HapClientNativeInterface.class.getSimpleName(); 31 32 @Native private final HapClientNativeCallback mHapClientNativeCallback; 33 HapClientNativeInterface(HapClientNativeCallback hapClientNativeCallback)34 public HapClientNativeInterface(HapClientNativeCallback hapClientNativeCallback) { 35 mHapClientNativeCallback = requireNonNull(hapClientNativeCallback); 36 } 37 connectHapClient(BluetoothDevice device)38 boolean connectHapClient(BluetoothDevice device) { 39 return connectHapClientNative(getByteAddress(device)); 40 } 41 disconnectHapClient(BluetoothDevice device)42 boolean disconnectHapClient(BluetoothDevice device) { 43 return disconnectHapClientNative(getByteAddress(device)); 44 } 45 getByteAddress(BluetoothDevice device)46 private static byte[] getByteAddress(BluetoothDevice device) { 47 if (device == null) { 48 return Utils.getBytesFromAddress("00:00:00:00:00:00"); 49 } 50 return Utils.getBytesFromAddress(device.getAddress()); 51 } 52 init()53 void init() { 54 initNative(); 55 } 56 cleanup()57 void cleanup() { 58 cleanupNative(); 59 } 60 selectActivePreset(BluetoothDevice device, int presetIndex)61 void selectActivePreset(BluetoothDevice device, int presetIndex) { 62 selectActivePresetNative(getByteAddress(device), presetIndex); 63 } 64 groupSelectActivePreset(int groupId, int presetIndex)65 void groupSelectActivePreset(int groupId, int presetIndex) { 66 groupSelectActivePresetNative(groupId, presetIndex); 67 } 68 nextActivePreset(BluetoothDevice device)69 void nextActivePreset(BluetoothDevice device) { 70 nextActivePresetNative(getByteAddress(device)); 71 } 72 groupNextActivePreset(int groupId)73 void groupNextActivePreset(int groupId) { 74 groupNextActivePresetNative(groupId); 75 } 76 previousActivePreset(BluetoothDevice device)77 void previousActivePreset(BluetoothDevice device) { 78 previousActivePresetNative(getByteAddress(device)); 79 } 80 groupPreviousActivePreset(int groupId)81 void groupPreviousActivePreset(int groupId) { 82 groupPreviousActivePresetNative(groupId); 83 } 84 getPresetInfo(BluetoothDevice device, int presetIndex)85 void getPresetInfo(BluetoothDevice device, int presetIndex) { 86 getPresetInfoNative(getByteAddress(device), presetIndex); 87 } 88 setPresetName(BluetoothDevice device, int presetIndex, String name)89 void setPresetName(BluetoothDevice device, int presetIndex, String name) { 90 setPresetNameNative(getByteAddress(device), presetIndex, name); 91 } 92 groupSetPresetName(int groupId, int presetIndex, String name)93 void groupSetPresetName(int groupId, int presetIndex, String name) { 94 groupSetPresetNameNative(groupId, presetIndex, name); 95 } 96 97 // Native methods that call into the JNI interface initNative()98 private native void initNative(); 99 cleanupNative()100 private native void cleanupNative(); 101 connectHapClientNative(byte[] address)102 private native boolean connectHapClientNative(byte[] address); 103 disconnectHapClientNative(byte[] address)104 private native boolean disconnectHapClientNative(byte[] address); 105 selectActivePresetNative(byte[] byteAddress, int presetIndex)106 private native void selectActivePresetNative(byte[] byteAddress, int presetIndex); 107 groupSelectActivePresetNative(int groupId, int presetIndex)108 private native void groupSelectActivePresetNative(int groupId, int presetIndex); 109 nextActivePresetNative(byte[] byteAddress)110 private native void nextActivePresetNative(byte[] byteAddress); 111 groupNextActivePresetNative(int groupId)112 private native void groupNextActivePresetNative(int groupId); 113 previousActivePresetNative(byte[] byteAddress)114 private native void previousActivePresetNative(byte[] byteAddress); 115 groupPreviousActivePresetNative(int groupId)116 private native void groupPreviousActivePresetNative(int groupId); 117 getPresetInfoNative(byte[] byteAddress, int presetIndex)118 private native void getPresetInfoNative(byte[] byteAddress, int presetIndex); 119 setPresetNameNative(byte[] byteAddress, int presetIndex, String name)120 private native void setPresetNameNative(byte[] byteAddress, int presetIndex, String name); 121 groupSetPresetNameNative(int groupId, int presetIndex, String name)122 private native void groupSetPresetNameNative(int groupId, int presetIndex, String name); 123 } 124