주요 4대 태스크
BluetoothAdapter
BluetoothDevice
BluetoothSocket
BluetoothServerSocket
BluetoothClass
PERMISSION (1)
| <mainfest ...> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH" /> </manifest ...> |
BLUETOOTH 퍼미션
BULETOOTH_ADMIN 퍼미션
BluetoothAdapter (2, 3)
1단계 : BluetoothAdapter 얻기
|
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter == null) { // Device가 Bluetooth를 지원하지 않고 있음 } |
2단계 : 블루투스 활성화
|
if(!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } |
Bluetooth Finding (4)
디바이스 검색 단계
패어드(Paired)와 커넥션(Connection)
패어드 디바이스 커리
|
// Get a set of currently paired devices // If there are paired devices, add each one to the ArrayAdapter |
디바이스 검색(Discovering) - Client의 역할
발견된 디바이스에 대한 정보를 수신하기 위해 ACTION_FOUND 인텐트에 대한 BroadcastReceiver를 등록해야 함
|
// The BroadcastReceiver that listens for discovered devices and // When discovery finds a device |
검색 가능(discoverablility) 활성화 - Server의 역할
그에 따라 디폴트로 120초 동안 검색 가능하게 설정되게 된다.
| if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); } |
지금 하는 부분