Kotlin How to init BluetoothDevice












1














I want to use bluetooth printer in my project, but I got this error



  lateinit property mmDevice has been not initialized


This is my code



lateinit var device:String
lateinit var mBluetoothAdapter: BluetoothAdapter
lateinit var mmSocket: BluetoothSocket
lateinit var mmDevice: BluetoothDevice

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_penjualan_cetak)

device = Function().getShared("printer","",this)

try {
findBT()
} catch (e: Exception) {

}
}

fun findBT(){
try{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

val paireddevice = mBluetoothAdapter.bondedDevices

if(paireddevice.size > 0){
ePrinter.setText("Printer Belum Dipilih")
for (device:BluetoothDevice in paireddevice) {
if (device.name == this.device) {
// this is the error come from
mmDevice = device
break
}
}
}
}catch (e:Exception){
e.printStackTrace()
}
}


How can I init the bluetoothdevice in koltin?, I have tried some solution but it not works










share|improve this question






















  • Are you sure the error is coming from this line? You don't use mmDevice at any other position in your code?
    – Christopher
    Nov 20 at 13:17










  • Agree with @Christopher. Anyway you can change your mmDevice to nullable type and get rid of lateinit: var mmDevice: BluetoothDevice? = null
    – Andrei Vinogradov
    Nov 20 at 13:20
















1














I want to use bluetooth printer in my project, but I got this error



  lateinit property mmDevice has been not initialized


This is my code



lateinit var device:String
lateinit var mBluetoothAdapter: BluetoothAdapter
lateinit var mmSocket: BluetoothSocket
lateinit var mmDevice: BluetoothDevice

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_penjualan_cetak)

device = Function().getShared("printer","",this)

try {
findBT()
} catch (e: Exception) {

}
}

fun findBT(){
try{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

val paireddevice = mBluetoothAdapter.bondedDevices

if(paireddevice.size > 0){
ePrinter.setText("Printer Belum Dipilih")
for (device:BluetoothDevice in paireddevice) {
if (device.name == this.device) {
// this is the error come from
mmDevice = device
break
}
}
}
}catch (e:Exception){
e.printStackTrace()
}
}


How can I init the bluetoothdevice in koltin?, I have tried some solution but it not works










share|improve this question






















  • Are you sure the error is coming from this line? You don't use mmDevice at any other position in your code?
    – Christopher
    Nov 20 at 13:17










  • Agree with @Christopher. Anyway you can change your mmDevice to nullable type and get rid of lateinit: var mmDevice: BluetoothDevice? = null
    – Andrei Vinogradov
    Nov 20 at 13:20














1












1








1







I want to use bluetooth printer in my project, but I got this error



  lateinit property mmDevice has been not initialized


This is my code



lateinit var device:String
lateinit var mBluetoothAdapter: BluetoothAdapter
lateinit var mmSocket: BluetoothSocket
lateinit var mmDevice: BluetoothDevice

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_penjualan_cetak)

device = Function().getShared("printer","",this)

try {
findBT()
} catch (e: Exception) {

}
}

fun findBT(){
try{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

val paireddevice = mBluetoothAdapter.bondedDevices

if(paireddevice.size > 0){
ePrinter.setText("Printer Belum Dipilih")
for (device:BluetoothDevice in paireddevice) {
if (device.name == this.device) {
// this is the error come from
mmDevice = device
break
}
}
}
}catch (e:Exception){
e.printStackTrace()
}
}


How can I init the bluetoothdevice in koltin?, I have tried some solution but it not works










share|improve this question













I want to use bluetooth printer in my project, but I got this error



  lateinit property mmDevice has been not initialized


This is my code



lateinit var device:String
lateinit var mBluetoothAdapter: BluetoothAdapter
lateinit var mmSocket: BluetoothSocket
lateinit var mmDevice: BluetoothDevice

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_penjualan_cetak)

device = Function().getShared("printer","",this)

try {
findBT()
} catch (e: Exception) {

}
}

fun findBT(){
try{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

val paireddevice = mBluetoothAdapter.bondedDevices

if(paireddevice.size > 0){
ePrinter.setText("Printer Belum Dipilih")
for (device:BluetoothDevice in paireddevice) {
if (device.name == this.device) {
// this is the error come from
mmDevice = device
break
}
}
}
}catch (e:Exception){
e.printStackTrace()
}
}


How can I init the bluetoothdevice in koltin?, I have tried some solution but it not works







android bluetooth kotlin kotlin-lateinit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 6 at 1:51









Firman

62




62












  • Are you sure the error is coming from this line? You don't use mmDevice at any other position in your code?
    – Christopher
    Nov 20 at 13:17










  • Agree with @Christopher. Anyway you can change your mmDevice to nullable type and get rid of lateinit: var mmDevice: BluetoothDevice? = null
    – Andrei Vinogradov
    Nov 20 at 13:20


















  • Are you sure the error is coming from this line? You don't use mmDevice at any other position in your code?
    – Christopher
    Nov 20 at 13:17










  • Agree with @Christopher. Anyway you can change your mmDevice to nullable type and get rid of lateinit: var mmDevice: BluetoothDevice? = null
    – Andrei Vinogradov
    Nov 20 at 13:20
















Are you sure the error is coming from this line? You don't use mmDevice at any other position in your code?
– Christopher
Nov 20 at 13:17




Are you sure the error is coming from this line? You don't use mmDevice at any other position in your code?
– Christopher
Nov 20 at 13:17












Agree with @Christopher. Anyway you can change your mmDevice to nullable type and get rid of lateinit: var mmDevice: BluetoothDevice? = null
– Andrei Vinogradov
Nov 20 at 13:20




Agree with @Christopher. Anyway you can change your mmDevice to nullable type and get rid of lateinit: var mmDevice: BluetoothDevice? = null
– Andrei Vinogradov
Nov 20 at 13:20












1 Answer
1






active

oldest

votes


















0














I think, that you did not post all your code and point us to a wrong line where error happen. This error can occur only when you trying to access not initialized lateinit property. I can imagine, that your findBT method is not finding any sutable device, so your mmDevice is not initialized.



You should use lateinit only when you 100% sure, that property will be initialized before first use. Searching BT device - not that case. So I suggest you to change that line:



lateinit var mmDevice: BluetoothDevice


to that line:



var mmDevice: BluetoothDevice? = null


and use null-checks or safe calls on it in your code.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51699668%2fkotlin-how-to-init-bluetoothdevice%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I think, that you did not post all your code and point us to a wrong line where error happen. This error can occur only when you trying to access not initialized lateinit property. I can imagine, that your findBT method is not finding any sutable device, so your mmDevice is not initialized.



    You should use lateinit only when you 100% sure, that property will be initialized before first use. Searching BT device - not that case. So I suggest you to change that line:



    lateinit var mmDevice: BluetoothDevice


    to that line:



    var mmDevice: BluetoothDevice? = null


    and use null-checks or safe calls on it in your code.






    share|improve this answer


























      0














      I think, that you did not post all your code and point us to a wrong line where error happen. This error can occur only when you trying to access not initialized lateinit property. I can imagine, that your findBT method is not finding any sutable device, so your mmDevice is not initialized.



      You should use lateinit only when you 100% sure, that property will be initialized before first use. Searching BT device - not that case. So I suggest you to change that line:



      lateinit var mmDevice: BluetoothDevice


      to that line:



      var mmDevice: BluetoothDevice? = null


      and use null-checks or safe calls on it in your code.






      share|improve this answer
























        0












        0








        0






        I think, that you did not post all your code and point us to a wrong line where error happen. This error can occur only when you trying to access not initialized lateinit property. I can imagine, that your findBT method is not finding any sutable device, so your mmDevice is not initialized.



        You should use lateinit only when you 100% sure, that property will be initialized before first use. Searching BT device - not that case. So I suggest you to change that line:



        lateinit var mmDevice: BluetoothDevice


        to that line:



        var mmDevice: BluetoothDevice? = null


        and use null-checks or safe calls on it in your code.






        share|improve this answer












        I think, that you did not post all your code and point us to a wrong line where error happen. This error can occur only when you trying to access not initialized lateinit property. I can imagine, that your findBT method is not finding any sutable device, so your mmDevice is not initialized.



        You should use lateinit only when you 100% sure, that property will be initialized before first use. Searching BT device - not that case. So I suggest you to change that line:



        lateinit var mmDevice: BluetoothDevice


        to that line:



        var mmDevice: BluetoothDevice? = null


        and use null-checks or safe calls on it in your code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 13:25









        Andrei Vinogradov

        619417




        619417






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51699668%2fkotlin-how-to-init-bluetoothdevice%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin