Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.uni.controllerpasstrough
- import AskBlActiv
- import PingResponseListener
- import android.app.Activity
- import android.bluetooth.BluetoothSocket
- import android.graphics.Color
- import android.os.Bundle
- import android.os.ParcelUuid
- import android.util.Log
- import android.view.View
- import android.view.ViewGroup
- import android.widget.AdapterView
- import android.widget.ArrayAdapter
- import android.widget.Button
- import android.widget.EditText
- import android.widget.ListView
- import android.widget.TextView
- import androidx.activity.result.ActivityResultLauncher
- import androidx.appcompat.app.AppCompatActivity
- import androidx.databinding.DataBindingUtil
- import blspySck
- import com.uni.controllerpasstrough.databinding.MainBinding
- import detect_bluetooth
- import java.io.InputStream
- import java.io.OutputStream
- class MainActivity : AppCompatActivity(),PingResponseListener {
- private lateinit var resultLauncher: ActivityResultLauncher<PingResponseListener>
- lateinit var ipInput: EditText
- lateinit var portInput: EditText
- lateinit var pingText: TextView
- lateinit var connection_button: Button
- lateinit var connect_list: ListView
- lateinit var statusText: TextView
- lateinit var serverIp: String
- var serverPort: Int = 0
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- val binding: MainBinding = DataBindingUtil.setContentView(this,R.layout.main)
- ipInput = binding.ipInp
- portInput = binding.portInp
- pingText = binding.pingText
- connection_button = binding.connnectionButton
- connect_list = binding.connectList
- statusText = binding.statusText
- var blDvList = detect_bluetooth().listDevices()
- //connection_button.setOnClickListener {
- // if (ipInput.text.isEmpty()) {
- // serverIp = "88.166.130.137"
- // } else {
- // serverIp = ipInput.toString()
- // }
- // if (portInput.text.isEmpty()) {
- // serverPort = 45150
- // } else {
- // serverPort = portInput.text.toString().toInt()
- // }
- // pingView.text= getString(R.string.pinging)
- // ping(serverIp, serverPort, this).startPing().toString()
- // }
- val selectedBlDv = mutableListOf<blDevice>()
- connect_list.adapter = DeviceAdapter(this, blDvList,selectedBlDv)
- connect_list.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
- val device = parent.getItemAtPosition(position) as blDevice
- if (device in selectedBlDv){
- selectedBlDv.remove(device)
- }
- else{
- selectedBlDv.add(device)
- }
- (parent.adapter as ArrayAdapter<String>).notifyDataSetChanged()
- }
- connection_button.setOnClickListener { //temp
- AskBlActiv(this).activateBluetooth()
- Thread { //"A8:8C:3E:62:8A:1C"
- lateinit var debugBlDv: blDevice
- for (i in blDvList) { //debug
- if (i.macAddr == "A8:8C:3E:62:8A:1C") {
- debugBlDv = i
- }
- }
- lateinit var macAddress: String
- lateinit var Uuids: List<ParcelUuid>
- if (selectedBlDv.size <= 1) {
- macAddress = debugBlDv.macAddr
- Uuids = debugBlDv.uuid
- }
- else {
- macAddress = selectedBlDv[0].macAddr
- Uuids = selectedBlDv[0].uuid
- }
- //TODO ADD SUPPORT MULTIPLE DEVICES
- var connectedSockets = mutableListOf<BluetoothSocket>()
- try {
- for (i in Uuids.indices){ //0 included to size not included
- Log.d("+-+","creating socket for " + Uuids[i])
- val sck = blspySck(macAddress,Uuids[i].toString()).getSocket()
- Log.d("+-+","Socket created")
- Thread.sleep(1000) //certainly useless - TO REMOVE
- //TODO remove that ↑
- try{
- sck!!.connect()
- connectedSockets.add(sck)
- }
- catch (e:Exception) {}
- Log.d("+-+","Socket connected")
- }
- val inputStreamList: MutableList<InputStream> = mutableListOf()
- val outputStreamList: MutableList<OutputStream> = mutableListOf()
- for (i in connectedSockets) {
- inputStreamList.add(i.inputStream)
- outputStreamList.add(i.outputStream)
- }
- }
- catch (e: Exception){
- Log.e("+-+ error", e.toString() +"\n"+ e.cause)
- }
- }.start()
- }
- }
- fun updateStatusText(){
- statusText.text = "Status: \nServer: Not Connected,\n$"
- }
- override fun onPingResponse(responseTime: String) {
- runOnUiThread {
- pingText.text = responseTime
- }
- }
- override fun onPingTimeout() {
- runOnUiThread {
- pingText.text = getString(R.string.timeout)
- }
- }
- }
- //TODO AJOUTER FOREGROUND SERVICE. 1H
- //TODO implem server
- //TODO FIX DEVICE SELECT. 1h?
- //TODO AJOUTER UDP LISTENER - USE CODE FROM PING - TEMP: LOG.i? 1-2h
- //TODO TEST COMMUNICATION DUAL WAY PC/PHONE ( déja fait ds ping ). 30min
- //TODO DEMANDER ALLUMAGE ET PERM BLUETOOTH QD APPUYER SUR CONNECT. 30min allumage 2-3h perm
- //TODO SEND LIST UUID / DEVICE TO PC. 1h
- //TODO AJOUTER SUPPORT APPAREIL SANS DEVICE APPAIRÉ - CURRENT: CRASH DIFF: SIMPLE (si vide -> ajouter une fake entrée "you have no....") 1h
- data class blDevice(val name:String, val macAddr: String, val uuid:List<ParcelUuid>){}
- data class server(val ip: String,val port:Int) {}
- class DeviceAdapter(context: Activity, private val devices: List<blDevice>, private val selectedDevices: MutableList<blDevice>) : ArrayAdapter<blDevice>(context, android.R.layout.simple_list_item_1, devices) {
- override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
- val view = super.getView(position, convertView, parent)
- val device = devices[position]
- if (DebugVar().selectDeviceListViewAdvanccedMode) {
- (view as TextView).text = "Name: ${device.name}\nMacAddress: ${device.macAddr}\nUuid: ${device.uuid}"
- }
- else{
- (view as TextView).text = device.name
- }
- view.setBackgroundColor(if (device in selectedDevices) Color.BLACK else Color.TRANSPARENT)
- return view
- }
- }
- class DebugVar(){
- var selectDeviceListViewAdvanccedMode = true
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement