Androiddd

Android Bluetooth GamePad 연결하기

Prod. No. 7 2014. 8. 14. 17:31

역시... Android API 는 설명이 잘되어 있다. : )


http://developer.android.com/guide/topics/connectivity/bluetooth.html

http://developer.android.com/training/game-controllers/controller-input.html


- AndroidManifest.xml 설정

<manifest ... >
 
<uses-permission android:name="android.permission.BLUETOOTH" />
  ...
</manifest>

- Bluetooth Adapter 설정

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
   
// Device does not support Bluetooth
}

- Bluetooth On

if (!mBluetoothAdapter.isEnabled()) {
   
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult
(enableBtIntent, REQUEST_ENABLE_BT);
}

- Paired 된 놈들 얻기

BondedDevice 가 pairing 된 놈들이다.

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
   
// Loop through paired devices
   
for (BluetoothDevice device : pairedDevices) {
       
// Add the name and address to an array adapter to show in a ListView
        mArrayAdapter
.add(device.getName() + "\n" + device.getAddress());
   
}
}

- 연결된 놈들 확인

 BroadcastReceiver 를 달면 onReceive 함수의 intent 로 정보가 날라옴

// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
   
public void onReceive(Context context, Intent intent) {
       
String action = intent.getAction();
       
// When discovery finds a device
       
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           
// Get the BluetoothDevice object from the Intent
           
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
           
// Add the name and address to an array adapter to show in a ListView
            mArrayAdapter
.add(device.getName() + "\n" + device.getAddress());
       
}
   
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver
(mReceiver, filter); // Don't forget to unregister during onDestroy

- InputDevice 객체를 얻어온후에 getName() 을 통해 이름 확인

BTDevice 의 이름과 InputDevice 의 이름을 가지고 match 를 시키면 되고...

해당 InputDevice 가 GamePad 인지 아닌지는 아래 소스로 확인하면 된다.

GamePad 만의 유니크한(?) 버튼을 가진 controller 인지 아닌지를 체크하는 방식.

public ArrayList getGameControllerIds() {
   
ArrayList gameControllerDeviceIds = new ArrayList();
   
int[] deviceIds = InputDevice.getDeviceIds();
   
for (int deviceId : deviceIds) {
       
InputDevice dev = InputDevice.getDevice(deviceId);
       
int sources = dev.getSources();

       
// Verify that the device has gamepad buttons, control sticks, or both.
       
if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
               
|| ((sources & InputDevice.SOURCE_JOYSTICK)
               
== InputDevice.SOURCE_JOYSTICK)) {
           
// This device is a game controller. Store its device ID.
           
if (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds
.add(deviceId);
           
}
       
}
   
}
   
return gameControllerDeviceIds;
}

끝.