Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 질투
- #만취
- HttpURLConnection
- 적절한 타이밍
- 슈퍼스타k 2016
- 해맑게 웃는 너
- 센티멘탈
- 사랑 그렇게 보내네 김영근
- account ban
- rsnapshot
- 보고싶은 내맘
- 그게 무슨 의미가 있니
- nmtui
- 김영근 올패스
- i7-8700
- 퇴근버스 조민욱
- CentOS 6 설치 오류
- 내 자신의 생각
- 가식의 껍데기
- 영구정지
- 흘러가는대로
- 흐린날의 낮술
- COC
- 커피레이크
- 퇴근버스 슈퍼스타K 2016
- 3rd party software
- 넬 희망고문
- 지리산 소울 김영근
- 짝사랑
- 도전의 기회
Archives
- Today
- Total
끄적거림들...
Android Bluetooth GamePad 연결하기 본문
역시... 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 ArrayListgetGameControllerIds() {
ArrayListgameControllerDeviceIds = 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;
}
끝.
'Androiddd' 카테고리의 다른 글
Android DB 사용하기 예제 (0) | 2016.09.05 |
---|---|
FTP 파일 업로드하기 (FTPClient) (0) | 2016.09.05 |
HttpURLConnection 예제 (0) | 2016.09.05 |
HttpClient vs HttpURLConnection (0) | 2016.09.05 |
This Android SDK requires ADT version 23.0.0 or above (0) | 2014.07.28 |
Comments