Advertisement
univers93

Untitled

Apr 21st, 2024
1,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.83 KB | Source Code | 0 0
  1. package com.uni.controllerpasstrough
  2. import AskBlActiv
  3. import PingResponseListener
  4. import android.app.Activity
  5. import android.bluetooth.BluetoothSocket
  6. import android.graphics.Color
  7. import android.os.Bundle
  8. import android.os.ParcelUuid
  9. import android.util.Log
  10. import android.view.View
  11. import android.view.ViewGroup
  12. import android.widget.AdapterView
  13. import android.widget.ArrayAdapter
  14. import android.widget.Button
  15. import android.widget.EditText
  16. import android.widget.ListView
  17. import android.widget.TextView
  18. import androidx.activity.result.ActivityResultLauncher
  19. import androidx.appcompat.app.AppCompatActivity
  20. import androidx.databinding.DataBindingUtil
  21. import blspySck
  22. import com.uni.controllerpasstrough.databinding.MainBinding
  23. import detect_bluetooth
  24. import java.io.InputStream
  25. import java.io.OutputStream
  26.  
  27. class MainActivity : AppCompatActivity(),PingResponseListener  {
  28.     private lateinit var resultLauncher: ActivityResultLauncher<PingResponseListener>
  29.     lateinit var ipInput: EditText
  30.     lateinit var portInput: EditText
  31.     lateinit var pingText: TextView
  32.     lateinit var connection_button: Button
  33.     lateinit var connect_list: ListView
  34.     lateinit var statusText: TextView
  35.     lateinit var serverIp: String
  36.     var serverPort: Int = 0
  37.  
  38.  
  39.  
  40.     override fun onCreate(savedInstanceState: Bundle?) {
  41.         super.onCreate(savedInstanceState)
  42.         val binding: MainBinding = DataBindingUtil.setContentView(this,R.layout.main)
  43.         ipInput = binding.ipInp
  44.         portInput = binding.portInp
  45.         pingText = binding.pingText
  46.         connection_button  = binding.connnectionButton
  47.         connect_list = binding.connectList
  48.         statusText = binding.statusText
  49.  
  50.         var blDvList = detect_bluetooth().listDevices()
  51.  
  52.         //connection_button.setOnClickListener {
  53.         //  if (ipInput.text.isEmpty()) {
  54.         //      serverIp = "88.166.130.137"
  55.         //  } else {
  56.         //      serverIp = ipInput.toString()
  57.         //  }
  58.         //  if (portInput.text.isEmpty()) {
  59.         //      serverPort = 45150
  60.         //  } else {
  61.         //      serverPort = portInput.text.toString().toInt()
  62.         //  }
  63.         //  pingView.text= getString(R.string.pinging)
  64.         //  ping(serverIp, serverPort, this).startPing().toString()
  65.         // }
  66.  
  67.  
  68.         val selectedBlDv = mutableListOf<blDevice>()
  69.  
  70.         connect_list.adapter = DeviceAdapter(this, blDvList,selectedBlDv)
  71.  
  72.         connect_list.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
  73.             val device = parent.getItemAtPosition(position) as blDevice
  74.             if (device in selectedBlDv){
  75.                 selectedBlDv.remove(device)
  76.             }
  77.             else{
  78.                 selectedBlDv.add(device)
  79.             }
  80.             (parent.adapter as ArrayAdapter<String>).notifyDataSetChanged()
  81.         }
  82.  
  83.  
  84.  
  85.         connection_button.setOnClickListener { //temp
  86.             AskBlActiv(this).activateBluetooth()
  87.             Thread { //"A8:8C:3E:62:8A:1C"
  88.  
  89.                 lateinit var debugBlDv: blDevice
  90.  
  91.                 for (i in blDvList) { //debug
  92.                     if (i.macAddr == "A8:8C:3E:62:8A:1C") {
  93.                         debugBlDv = i
  94.                     }
  95.                 }
  96.  
  97.                 lateinit var macAddress: String
  98.                 lateinit var Uuids: List<ParcelUuid>
  99.  
  100.                 if (selectedBlDv.size <= 1) {
  101.                     macAddress = debugBlDv.macAddr
  102.                     Uuids = debugBlDv.uuid
  103.                 }
  104.                 else {
  105.                     macAddress = selectedBlDv[0].macAddr
  106.                     Uuids = selectedBlDv[0].uuid
  107.                 }
  108.  
  109.                 //TODO ADD SUPPORT MULTIPLE DEVICES
  110.  
  111.                 var connectedSockets = mutableListOf<BluetoothSocket>()
  112.  
  113.                 try {
  114.                     for (i in Uuids.indices){ //0 included to size not included
  115.                         Log.d("+-+","creating socket for " + Uuids[i])
  116.                         val sck = blspySck(macAddress,Uuids[i].toString()).getSocket()
  117.                         Log.d("+-+","Socket created")
  118.  
  119.                         Thread.sleep(1000) //certainly useless - TO REMOVE
  120.                         //TODO remove that ↑
  121.  
  122.                         try{
  123.                             sck!!.connect()
  124.                             connectedSockets.add(sck)
  125.                         }
  126.                         catch (e:Exception) {}
  127.                         Log.d("+-+","Socket connected")
  128.                     }
  129.                     val inputStreamList: MutableList<InputStream> = mutableListOf()
  130.                     val outputStreamList: MutableList<OutputStream> = mutableListOf()
  131.                     for (i in connectedSockets) {
  132.                         inputStreamList.add(i.inputStream)
  133.                         outputStreamList.add(i.outputStream)
  134.                     }
  135.                 }
  136.                 catch (e: Exception){
  137.                     Log.e("+-+ error", e.toString() +"\n"+ e.cause)
  138.                 }
  139.             }.start()
  140.         }
  141.  
  142.     }
  143.     fun updateStatusText(){
  144.         statusText.text = "Status: \nServer: Not Connected,\n$"
  145.     }
  146.  
  147.     override fun onPingResponse(responseTime: String) {
  148.         runOnUiThread {
  149.             pingText.text = responseTime
  150.         }
  151.     }
  152.     override fun onPingTimeout() {
  153.         runOnUiThread {
  154.             pingText.text = getString(R.string.timeout)
  155.         }
  156.     }
  157. }
  158. //TODO AJOUTER FOREGROUND SERVICE. 1H
  159. //TODO implem server
  160.  
  161.  
  162. //TODO FIX DEVICE SELECT. 1h?
  163. //TODO AJOUTER UDP LISTENER - USE CODE FROM PING - TEMP: LOG.i? 1-2h
  164. //TODO TEST COMMUNICATION DUAL WAY PC/PHONE ( déja fait ds ping ). 30min
  165. //TODO DEMANDER ALLUMAGE ET PERM BLUETOOTH QD APPUYER SUR CONNECT. 30min allumage 2-3h perm
  166. //TODO SEND LIST UUID / DEVICE TO PC. 1h
  167. //TODO AJOUTER SUPPORT APPAREIL SANS DEVICE APPAIRÉ - CURRENT: CRASH DIFF: SIMPLE (si vide -> ajouter une fake entrée "you have no....") 1h
  168.  
  169. data class blDevice(val name:String, val macAddr: String, val uuid:List<ParcelUuid>){}
  170.  
  171. data class server(val ip: String,val port:Int) {}
  172.  
  173. 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) {
  174.     override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
  175.         val view = super.getView(position, convertView, parent)
  176.         val device = devices[position]
  177.  
  178.         if (DebugVar().selectDeviceListViewAdvanccedMode) {
  179.             (view as TextView).text = "Name: ${device.name}\nMacAddress: ${device.macAddr}\nUuid: ${device.uuid}"
  180.         }
  181.         else{
  182.             (view as TextView).text = device.name
  183.         }
  184.         view.setBackgroundColor(if (device in selectedDevices) Color.BLACK else Color.TRANSPARENT)
  185.         return view
  186.     }
  187. }
  188.  
  189. class DebugVar(){
  190.     var selectDeviceListViewAdvanccedMode = true
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement