참고

developer.android.com/guide/topics/connectivity/bluetooth?hl=ko

 

블루투스 개요  |  Android 개발자  |  Android Developers

The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth…

developer.android.com

 

 

1. 권한 설정 

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- If your app targets Android 9 or lower, you can declare ACCESS_COARSE_LOCATION instead. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

 

 

2. 블루투스 지원되는지 확인  및 블루투스 활성화 

mBlueAdapter = BluetoothAdapter.getDefaultAdapter()

//check if bluetooth is available or not
if (mBlueAdapter == null) {
	showToast("bluetooth is not available")
    finish()
 } else {
 	mBlueAdapter?.let {
    	if (!it.isEnabled()) {
        	showToast("Turning On Bluetooth...")
        	//intent to on bluetooth
        	val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
        	startActivityForResult(intent, REQUEST_ENABLE_BT)
        } else {
       		 showToast("Bluetooth is already on")
        }
	}
}

 

 

 

 

 

3. 페어링된 기기 목록 

val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
pairedDevices?.forEach { device ->
    val deviceName = device.name
    val deviceHardwareAddress = device.address // MAC address
}

 

 

4. 클라이언트로 연결  

private inner class ConnectThread(device: BluetoothDevice) : Thread() {
    private val mmSocket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
        device.createRfcommSocketToServiceRecord(MY_UUID)
    }

    public override fun run() {
        // Cancel discovery because it otherwise slows down the connection.
        bluetoothAdapter?.cancelDiscovery()
        mmSocket?.use { socket ->
            // Connect to the remote device through the socket. This call blocks
            // until it succeeds or throws an exception.
            socket.connect()
            // The connection attempt succeeded. Perform work associated with
            // the connection in a separate thread.
            manageMyConnectedSocket(socket)
        }
    }

    // Closes the client socket and causes the thread to finish.
    fun cancel() {
        try {
            mmSocket?.close()
        } catch (e: IOException) {
            Log.e(TAG, "Could not close the client socket", e)
        }
    }
}

 

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
function deepCopy(array) {
    return JSON.parse(JSON.stringify(array));
}
 
var ascArr = [];
ascArr.push({'id':'id1''step':'1'});
ascArr.push({'id':'id2''step':'5'});
ascArr.push({'id':'id3''step':'2'});
ascArr.push({'id':'id4''step':'3'});
ascArr.push({'id':'id5''step':'2'});
ascArr.push({'id':'id6''step':'4'});
ascArr.push({'id':'id7''step':'1'});
 
// 오름차순
//compareFunction(a, b)이 0보다 작은 경우 a를 b보다 낮은 색인으로 정렬합니다. 즉, a가 먼저옵니다.
ascArr.sort(function(a, b) { 
    return a.step - b.step;
});
 
var descArr = deepCopy(ascArr);
// 내림차순
//compareFunction(a, b)이 0보다 큰 경우, b를 a보다 낮은 인덱스로 소트합니다.
descArr.sort(function(a, b) { 
    return b.step - a.step;
});
 
 
cs

HTML에서 Table을 세로값이 같으면 cell merge가 되어야 보기좋게 표현할 수 있습니다.
그려진 table에 대해서 동적으로 값을 비교하면서 Rowspan을
계산하여 적용하는 함수입니다.


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
32
33
34
35
36
37
38
39
function dynamicRowSpan() {
    var table = document.getElementById("table");
    var trArr = table.getElementsByTagName("tr");
    var thCnt = table.rows[0].getElementsByTagName("th").length;
    
    for (var tdIdx = thCnt-1; tdIdx >= 0; tdIdx--) {
        var rowSpan = 0;
        var compText = '';
        for (var trIdx = 1; trIdx < trArr.length; trIdx++) {
            var td = table.rows[trIdx].cells[tdIdx]; 
            if (compText == '') {
                compText = td.outerText;
                continue;
            }
            if (compText == td.outerText) {
                rowSpan++;
                td.setAttribute("class""del");
            } else {
                var td = table.rows[trIdx-1-rowSpan].cells[tdIdx];
                td.setAttribute("rowspan", rowSpan+1);
                rowSpan = 0;
                compText = table.rows[trIdx].cells[tdIdx].outerText;
            }
 
            if ( trIdx == trArr.length-1 && rowSpan > 0 ) {
                var cell = table.rows[trIdx-rowSpan].cells[tdIdx];
                cell.setAttribute("rowspan", rowSpan+1);
                rowSpan = 0;
                compText = '';
            }
        }
    }
 
    table = document.getElementById("table");
    var dels = table.getElementsByClassName("del");
    for(var i = dels.length-1; i >= 0; i--){
        dels[i].remove();
    }
}
cs

+ Recent posts